【无标题】

@[toc]

HelloWorld!

public class Hello {

	public static void main(String[] args) {
		System.out.println("Hello World!");
	}
}
运行结果
Hello World!

标识符和关键字

public class 标识符和关键字 {

	public static void main(String[] args) {
		int x,y,z;
		System.out.println("请输入两个整数:");
		Scanner in = new Scanner(System.in);
		x = in.nextInt();
		y = in.nextInt();
		z = x + y;
		System.out.println(x + "+" + y + "=" + z);
	}
}
运行结果:
2 + 3 = 5

变量

public class test {

	public void pupAge() {
		int age=0;
		age += 7;
		System.out.println("小狗的年龄是:" +age);
	}
	public static void main(String[] args) {
		test test = new test();
		test.pupAge();
	}
}
运行结果:
小狗的年龄是:7

自动类型转换

public class 自动类型转换 {

	public static void main(String[] args) {
		int i1 = 10;            	System.out.println(i1);      
		short s1 = 2;				System.out.println(s1);
		int i2 = i1 + s1;			System.out.println(i2);
		float f1 = 12.5f;			System.out.println(f1);
		float f2 = f1 + i2;			System.out.println(f2);
		long l = 12l;				System.out.println(l);
		float f3 = 1;				System.out.println(f3);
		char c1 = 'a';				System.out.println(c1);
		char c2 = 'A';				System.out.println(c2);
		int i3 = c1 + 1;			System.out.println(i3);
		int i4 = c2 + 1;			System.out.println(i4);
		short ss1 = 12;				System.out.println(ss1);
		byte bb1 = 1;				System.out.println(bb1);
		char cc1 = 'a';				System.out.println(cc1);
		//int iil = ss1 + bb1 + cc1;	System.out.println(ii1);	
	}
}
运行结果:
10 2 12 12.5 24.5 12 1.0 a A 98 66 12 1 a
注意:

1.容量小的数据类型与容量大的数据类型做运算时,容量小的数据类型会自动转换成容量大的数据类型

2.容量从小到大:char,byte,short --> int --> long --> float --> double

3.char类型和数字类型做运算时,会根据ASCCII码表把char类型转化为对应的int类型数据来运算

4.char,byte和short这三类之间做运算的结果默认自动转化为int类型的数据

问题:

为什么最后一个报错?

强制类型转换

public class 强制类型转换 {

	public static void main(String[] args) {
		String str1 = "abc";
		int il = 123;
		String str2 = str1 + il;
		System.out.println(str2);
	}
}
运行结果:
123abc
注意:字符串与基本数据类型之间只能进行连接运算,即将两个结果拼接到一起,得到的结果依旧是一个字符串类型的数据

顺序结构

public class 顺序结构 {

	public static void main(String[] args) {
		float f,c;
		f = 70.0f;
		c = 5 * (f - 32)/9;
		System.out.println("Fahrenheit = " +f);
		System.out.println("Centigrade = " +c);
	}
}
运行结果:
Fahrenheit = 70.0
Centigrade = 21.11111

分支结构

1.计算税款(if语句)
import java.util.Scanner;
public class if语句 {

	public static void main(String[] args) {
		double income = 0.0;
		int status = 0;
		double tax = 0;
		System.out.println("请输入纳税人的类型:0-单身,1-已婚,2-家庭");
		Scanner inStatus = new Scanner(System.in);
		if(inStatus.hasNextInt()) {
			status  = inStatus.nextInt();}
			System.out.println("请输入可征税收入:");
			Scanner in = new Scanner(System.in);
			if(in.hasNextDouble()) {
				income = in.nextDouble();
			}
			if(status == 0) {
				if(income <= 6000)
					tax = income * 0.10;
				else if(income <= 27950)
					tax = 6000 * 0.10 + (income - 6000)*0.15;
				else if(income <= 67700)
					tax = 6000 * 0.10 + (income - 27500)*0.27;
				else if(income <= 141250)
					tax = 6000 * 0.10 + (27950 - 6000) * 0.15 + (67700 - 27950) * 0.27 + (income - 67700) * 0.30;
				else 
					tax = 6000 * 0.10 +(27950 - 6000) * 0.15 + (67500 - 27950) * 0.27 + (141250 - 67700) * 0.30 + (income - 141250) * 0.35;
	}
			else if(status == 1) {
				tax = income * 0.2;
				//此处简便运算
			}
			else if(status == 2) {
				tax = income *0.25;
				//此处简便运算
			}System.out.println("纳税人需要交税的税额为" + tax + "¥");}
}
运行结果
请输入纳税人的类型:0-单身,1-已婚,2-家庭
0
请输入可征税收入:
10000
纳税人需要交税的税额为1200.0¥
运行结果:
请输入纳税人的类型:0-单身,1-已婚,2-家庭
1
请输入可征税收入:
10000
纳税人需要交税的税额为2000.0¥
运行结果:
请输入纳税人的类型:0-单身,1-已婚,2-家庭
2
请输入可征税收入:
10000
纳税人需要交税的税额为2500.0¥
2.计算税款(switch语句)
public class switch语句 {
	
	double income = 0.0;
	int status = 0;
	double tax = 0.0;
	System.out.println("请输入纳税人的类型:0-单身,1-已婚,2-家庭");
	Scanner inStatus = new Scanner(System.in);
	switch(status) {
	case 0: tax = income*0.15;
	break;
	case 1: tax = income*0.20;
	break;
	case 3: tax = income*0.25;
	break;
	}
	System.out.println("纳税人需要缴纳的税额为" + tax + "¥");
	
}
运行结果
请输入纳税人的类型:0-单身,1-已婚,2-家庭
0
请输入可征税收入:
10000
纳税人需要交税的税额为1500.0¥
运行结果:
请输入纳税人的类型:0-单身,1-已婚,2-家庭
1
请输入可征税收入:
10000
纳税人需要交税的税额为2000.0¥
运行结果:
请输入纳税人的类型:0-单身,1-已婚,2-家庭
2
请输入可征税收入:
10000
纳税人需要交税的税额为2500.0¥

输出月份的天数switch语句

import java.util.Scanner;

public class 输出月份的天数switch语句 {

	public static void main(String[] args) {
		System.out.println("请输入年份");
		Scanner inYear = new Scanner(System.in);
		int year = inYear.nextInt();//输入年份
		System.out.println("请输入年份");
		Scanner inMouth = new Scanner(System.in);//输入月份
		int mouth = inMouth.nextInt();
		int numDays = 0;
		switch(mouth) {//以月份作为分支条件
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:numDays = 31;
		break;
		case 4:
		case 6:
		case 9:
		case 11:numDays = 30;
		break;
		case 2://对2月 根据闰年判断天数
			if(((year%4 == 0)&&!(year%100 ==0))||(year%400 == 0)) {
				numDays = 29;
			}
			else {
				numDays = 28;
			}
		break;
		}
		System.out.println(year + "年" + mouth + "月份" + "有" + numDays + "天");
	}
}
运行结果:
请输入年份
2022
请输入年份
3
2022年3月份有31天

for语句Game

import java.util.Scanner;
public class for语句Game {

	static final int FGVALUE = 20;
	static final int GSVALUE = 16;//定义售价
	static final int FGLIFE = 30;
	static final int GSLIFE = 20;//定义增加的生命力
	public static void main(String[] args) {
		int goldcoin = 100; //定义金币数量
		int fg_num = 0,gs_num = 0;//定义FG和GS数量 
		int max_life = 0;//定义最大生命力值
		System.out.println("请输入金币的数量为:");
		Scanner in = new Scanner(System.in);
		goldcoin = in.nextInt();
		for(int fg_loop = 0;fg_loop <=(goldcoin/FGVALUE);fg_loop++)
			for(int gs_loop = 0;gs_loop <=(goldcoin/FGVALUE);gs_loop++)
				if(((fg_loop * FGVALUE + gs_loop * GSVALUE) <= goldcoin)
						&&((FGLIFE * fg_loop + GSLIFE * gs_loop) >= max_life)) {
					fg_num = fg_loop;//记录FG的数量
		gs_num = gs_loop;//记录GS的数量
						max_life = FGLIFE * fg_loop + GSLIFE * gs_loop;
				}
		System.out.println("购买的宝物最多能增加你" + max_life + "个生命力!");
		System.out.println("购买的FG的数量为" +fg_num);
		System.out.println("购买的GS的数量为" +gs_num);
		}
}
运行结果:
请输入金币的数量为:
36
购买的宝物最多能增加你50个生命力!
购买的FG的数量为1
购买的GS的数量为1

使用循环输出1-100的和

1.for语句
public class 用for循环输出1to100的和 {

	public static void main(String[] args) {
		int i,s = 0;
		for(i=1; i<=100; i++)
			s+=i;
		System.out.println("sum=" +s);
	}
}
2.while语句
public class 用while循环输出1to100的和 {

	public static void main(String[] args) {
		int i,s;
		i=1;s=0;
		while( i<=100 ) {
			s+=i;
			i+=1;
		}
		System.out.println("sum=" +s);
	}
}
3.do-while语句
public class 用dowhile循环输出1to100的和 {

	public static void main(String[] args) {
		int i=1,s=0;
		do {
			s+=i;
			i+=1;
		}while(i<=100);
		System.out.println("sum=" +s);
	}
}
运行结果:
5050

使用跳转语句

1.break语句 -- 跳出循环
public class break语句 {

	public static void main(String[] args) {
		int i=0;
		while(i<10) {
			i++;
			if(i == 5) {
				break;
			}
			System.out.println(i);
		}
		System.out.println("循环结束");
	}
}
运行结果:
1
2
3
4
循环结束
2.continue语句 -- 后面的不再执行,但循环不停止
public class continue语句 {

	public static void main(String[] args) {
		int i = 0;
		while(i < 4) {
			i++;
			if(i == 2) {
				continue;
			}
			System.out.println(i);
		}
		System.out.println("循环结束");
	}
}
运行结果:
1
3
4
循环结束
3.return语句 -- 后面的代码不被执行
public class return语句 {

	public static void main(String[] args) {
		int i = 0;
		while(i < 4) {
			i++;
			if(i == 3) {
				return;
			}
			System.out.println(i);
		}
		System.out.println("循环结束");
	}
}
运行结果:
1
2

JAVA数据的输出

1.System.out.println()
import javax.swing.plaf.synth.SynthFormattedTextFieldUI;

public class JAVA数据的输出 {

	public static void main(String[] args) {
		System.out.println("欢迎学习JAVA");
		System.out.println(10 * 2 + 4);
		System.out.println("a=" +20);
	}
}
运行结果:
欢迎学习JAVA
24
a=20
2.System.out.print()
import javax.swing.plaf.synth.SynthFormattedTextFieldUI;

public class 数据输出 {

	public static void main(String[] args) {
		System.out.print("欢迎学习JAVA");
		System.out.print(10 * 2 + 4);
		System.out.println("a=" +20);
		System.out.println("b=" +30);
	}
}
运行结果:
欢迎学习JAVA24a=20
b=30
注意:现已发现最大的区别就是print输出后没换行

数据的输入

1.使用命令行参数
public class 使用命令行参数 {

	final static double PI = 3.1415926;
	public static void main(String[] args) {
		double r,perimeter,area;
		r = Double.parseDouble(args[0]);
		perimeter = 2* PI *r;
		area = PI * r * r;
		System.out.println("圆的周长为:" +perimeter);
		System.out.println("圆的面积为:" +area);
	}
}
运行结果:
2
圆的周长为:12.5663704
圆的面积为:12.5663704
2.Scanner
import java.util.*;
public class Scanner {

	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);
		System.out.print("请输入你的姓名");
		String name = in.nextLine();
		System.out.println("输入你的年龄");
		int age = in.nextInt();
		System.out.println("你的姓名" "+name",年龄:" +age);
	}
}
运行结果:
请输入你的姓名:Jiawei Chen
输入你的年龄:18
你的姓名:Jiawei Chen,年龄:18

用JOptionPane类实现各种对话框

1.使用Javax.swing.JOptionPane包的showMessageDialog()方法
import javax.swing.JOptionPane;
public class 用JOptionPane类实现各种对话框 {

    public static void main(String[] args) {
		JOptionPane.showMessageDialog(null,"学习使我快乐");
		System.exit(0);
	}
}
2.使用javax.swing.JOptionPane类的showInputDialog()方法输入字符串
import javax.swing.*;
public class 用JOptionPane类实现各种对话框 {

	public static void main(String[] args) {
		int i,j,max;
		String s1,s2;
		s1 = JOptionPane.showInputDialog(null,"输入第1个整数");
				s2 = JOptionPane.showInputDialog(null,"输入第2个整数");
				i = Integer.parseInt(s1);
				j = Integer.parseInt(s2);
				max = i > j ? i : j;
				JOptionPane.showMessageDialog(null, i+ "和" +j+ "的最大数是" +max);
				System.exit(0);
	}
}

两种计算JAVA程序运行时间的方法

1.System.currentTimeMillis() 返回时间单位为毫秒 2.System.currentTimeMillis() 返回时间单位为纳秒
public class JAVA程序运行时间的计算 {

	public static void main(String[] args) {
		System.out.println(currentTimeMillis() + "ms");
		//毫秒时间
		System.out.println(nanoTime() + "ns");
		//纳秒时间
	}
	//Test毫秒时间
	public static long currentTimeMillis() {
		long starTime = System.currentTimeMillis();//获取开始时间
		int sum = 0;
		for(int i=0;i<10000000;i++) {
			sum+=i;
		}
		long endTime = System.currentTimeMillis();//获取结束时间
		return endTime - starTime;
	}
	//Test纳秒时间
	public static long nanoTime() {
		long starTime = System.nanoTime();//获取开始时间
		int sum = 0;
		for(int i=0; i<10000000; i++) {
			sum+=i;
		}
		long endTime = System.nanoTime();//获取结束时间
		return endTime - starTime;
	}
}
运行结果:
4ms
3965900ns

基于控制台设计简易打折与累加计算器

1.计算一次加法

public class 基于控制台设计简易打折与累加计算器 {
	
	public static void main(String[] args) {
		System.out.println("欢迎使用陈家伟的计算器");
		System.out.print("请输入第一个整数:");
		Scanner scanner = new Scanner(System.in);
		int data1 = scanner.nextInt();
		System.out.print("请输入第二个整数:");
		int data2 = scanner.nextInt();
		int date = date1 + date2;
		System.out.println("计算结果是:" +data);
	}
}
运行结果:
欢迎使用陈家伟的计算器
请输入第一个整数:21
请输入第二个整数:12
计算结果是:33

2.改成四则运算,并带有循环,直到想退出为止

import java.util.Scanner;
public class 基于控制台设计简易打折与累加计算器 {
    public static void main(String[] args){
        System.out.println("欢迎使用陈家伟的计算器");
        while(true){
            if(new 基于控制台设计简易打折与累加计算器().input_output()){
            }
            else{
                System.out.println("谢谢使用计算器OVER");
                break;
            }
        }
    }

    int count_data(int data1, int data2, String sign) {
        int data = 0;
        if (sign.equals("+")) {
            data = data1 + data2;
        } else if (sign.equals("-")) {
            data = data1 - data2;
        } else if (sign.equals("*")) {
            data = data1 * data2;
        } else if (sign.equals("/")) {
            data = data1 / data2;
        } else {
            data = -999999999;
        }
        return data;
    }
    
    boolean input_output() {
        System.out.println("请输入第一个整数");
        Scanner scanner = new Scanner(System.in);
        String l = scanner.nextLine();
        int data1 = Integer.valueOf(l);
        System.out.println("请输入运算符号:");
        String sign = scanner.nextLine();
        System.out.println("请输入第二个数字:");
        int data2 = Integer.valueOf(scanner.nextLine());
        int data = new 基于控制台设计简易打折与累加计算器().count_data(data1, data2, sign);
        if (data == -999999999) {
            System.out.println("计算结果错误:");
        } else {
            System.out.println("计算结果是:" + data);
        }
        System.out.println("是否继续Y/N:");
        String contu = scanner.nextLine();
        if ("Y".equals(contu) || "y".equals(contu)) {
            return true;
        } else {
            return false;
        }
    }
}
运行结果:
欢迎使用陈家伟的计算器
请输入第一个整数
21
请输入运算符号:
+
请输入第二个数字:
12
计算结果是:33
是否继续Y/N:
Y
请输入第一个整数
21
请输入运算符号:
请输入第二个数字:
12
计算结果是:9
是否继续Y/N:
Y
请输入第一个整数
21
请输入运算符号:

请输入第二个数字:
12
计算结果是:252
是否继续Y/N:
Y
请输入第一个整数
21
请输入运算符号:
/
请输入第二个数字:
12
计算结果是:1
是否继续Y/N:
N
谢谢使用计算器OVER

方法调用

1.方法表达式
public class 方法调用(方法表达式) {

	    static int square(int x) {
	        int s;
	        s = x * x;
	        return (s);
	    }

	    public static void main(String[] args) {
	        int n = 5;
	        int result = square(n);
	        System.out.println(result);
	}
}
2.方法语句
public class 方法调用 {

	    static void square(int x) {
	    	int s;
	    	s = x * x;
	    	System.out.println(s);
	    }
	    public static void main(String[] args) {
			int n=5;
			square(n);
		}
}
运行结果:
25

方法参数值传递--单向传递

public class 方法参数值传递单向传递 {

	static void swap(int x,int y) {
		int temp;
		System.out.println("Before Swapping");
		System.out.println("x=" +x+ "y=" +y);
		temp = x; x = y; y = temp;
		System.out.println("After Swapping");
		System.out.println("x=" +x+ "y=" +y);
	}
	public static void main(String[] args) {
		int u = 50,v = 100;
		System.out.println("Before Calling");
		System.out.println("u=" +u+ "v=" +v);
		swap(u,v);
		System.out.println("After Calling");
		System.out.println("u=" +u+ "v=" +v);
	}
}
运行结果:
Before Calling
u=50 v=100
Before Swapping
x=50 y=100
After Swapping
x=100 y=50
After Calling
u=50 v=100

输入输出基本类型数据

import java.util.Scanner;
public class 输入基本类型数据 {

	public static void main(String[] args) {
		System.out.println("请输入若干个数,每输入一个数回车确认");
		System.out.println("最后输入数字0结束输入操作");
		Scanner reader = new Scanner(System.in);
		double sum = 0;
		int m = 0;
		double x = reader.nextDouble();
		while(x!=0) {
			m = m + 1;
			sum = sum + x;
			x = reader.nextDouble();
		}
		System.out.println(m + "个数和为" +sum);
		System.out.println(m + "个数的平均值为" +sum/m);
	}
}
运行结果:
请输入若干个数,每输入一个数回车确认
最后输入数字0结束输入操作
1
2
3
4
5
6
7
8
9
0
9个数和为45.0
9个数的平均值为5.0

方法签名与方法重载

class 方法签名与方法重载{

	void test() {
		System.out.println("No parameters");
	}
	void test(int a) {
		System.out.println("a:" +a);
	}
	void test(int a,int b) {
		System.out.println("a: "+a+"b: "+b);
	}
	double test(double a) {
		System.out.println("double a:" +a);
		return a*a;
	}
}
class Overload{
	public static void main(String[] args) {
		方法签名与方法重载 ob = new 方法签名与方法重载();
		double result;
		ob.test();
		ob.test(10);
		ob.test(10.20);
		result = ob.test(123.22);
		System.out.println("Result of ob.test(123.22):" +result);
	}
}
问题:有一点点没看懂

设计计算购买商品总金额程序

public class 设计计算购买商品总金额程序 {
	 public static void add(double sumMoney) {
	            int price=0;
	  if(sumMoney>=1000) {
	   sumMoney *= 0.8;
	   price = 200;
	  }
	  else if(sumMoney>=500) {
	   sumMoney *= 0.85;
	   price = 100;
	  }
	  else if(sumMoney>=300) {
	   sumMoney *= 0.9;
	   price = 70;
	  }
	  else {
	   sumMoney *= 0.95;
	  }
	  System.out.printf("实际付款金额:%8.2f",sumMoney);
	  System.out.printf("获取购物金卷:",price);
	 }
	 public static void add(int sumMoney) {
	             int price=0;
	  if(sumMoney>=1000) {
	   sumMoney *= 0.8;
	   price = 200;
	  }
	  else if(sumMoney>=500) {
	   sumMoney *= 0.85;
	   price = 100;
	  }
	  else if(sumMoney>=300) {
	   sumMoney *= 0.9;
	   price = 70;
	  }
	  else {
	   sumMoney *= 0.95;
	  }
	  System.out.printf("实际付款金额:%d",sumMoney);
	  System.out.printf("获取购物金卷:%d",price);
	 }
	 public static void main(String[] args) {
		设计计算购买商品总金额程序 md = new 设计计算购买商品总金额程序();
		设计计算购买商品总金额程序.add(5000.0);
	  System.out.println();
	  设计计算购买商品总金额程序.add(5000);
	 }
}
运行结果:
实际付款金额: 4000.00获取购物金卷:200
实际付款金额:4000获取购物金卷:200
注意:采用分支结构对不同区域的值进行不同的运算

一维数组

1.初始化数组对象
public class 初始化数组对象 {

	public static void main(String[] args){
		int []age=new int[10];
		for(int i=0;i<age.length;i++){
			age[i]=i;
			System.out.print(age[i]+"    ");
		}
	}
}
运行结果:
0 1 2 3 4 5 6 7 8 9
2.访问数组元素
public class 访问数组元素 {

	public static void main(String[] args){
		int[] array={5,7,2,4};
		for(int j=0;j<4;j++){
		System.out.println(array[j]);
		}
	}
}
运行结果:
5
7
2
4

一维数组声明,创建,元素赋值三合一

1.先声明再使用
public class 先声明再使用 {

	public static void main(String[] args){
		int[] hobbies;
		hobbies=new int[3];
		hobbies[0]=1;
		hobbies[2]=2;
		System.out.println(hobbies[0]);
		System.out.println(hobbies.length);
	}
}
运行结果:
1
3
2.声明并创建的用法
public class 声明并创建的用法 {

	public static void main(String[] args){
	int[] moneys=new int[3];
	moneys[0]=1;
	moneys[1]=5;
	System.out.println(moneys.length);
	System.out.println(moneys[0]);
	}
}
运行结果:
3
1
3.声明,创建并赋值
public class 先声明再使用 {
	public static void main(String[] args){
		int[] moneys=new int[]{1,5,10};
		for(int i=0;i<moneys.length;i++){
		System.out.println(moneys[i]);
		}
	}
}
运行结果:
1
5
10

二维数组

1.二维数组初始化
public class 二维数组初始化 {

	public static void main(String[] args) {
		int [][] arr = new int[][]{{4,5,6,8},{2,3},{1,6,9}};
		System.out.println(arr.length);
		System.out.println(arr[0].length);
	}
}
运行结果:
3
4
2.二维数组遍历
1.双重for循环遍历
public class 双重for循环遍历 {

	public static void main(String[] args) {
		int[][] arr = new int[2][3];
		arr[0] = new int [] {1,2,3};
		arr[0][1] = 22;
		arr[1][1] = 13;
		arr[1][2] = 81;
		for(int i=0;i<arr.length;i++) {
			//System.out.println(arr[i]);//arr中2个数组的地址
			//遍历arr[0],arr中第一个数组
			for(int j=0;j<arr[i].length;j++) {
				System.out.println(arr[i][j] + ",");
			}
		}
	}
}
运行结果:
1,
22,
3,
0,
13,
81,
2.增强for循环foreach遍历

public class 增强for循环foreach遍历 {

public class 增强for循环foreach遍历 {

	public static void main(String[] args) {
		int [][] arr = {{65,6},{12,1,45,23},{0,-45,1}};
		for(int[]is : arr ) {
			for(int i : is) {
				System.out.println(i +",");
			}
		}
	}
}
运行结果:
65,
6,
12,
1,
45,
23,
0,
-45,
1,

基于控制台的学生成绩统计系统

暂时没做完

习题

1.数组转置
public class 数组转置 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        int i;
        for (i = 0; i < arr.length; i++) {
            arr[i] = in.nextInt();
        }
        for (i = arr.length - 1; i >= 0; i--) {
            System.out.printf("%d ", arr[i]);
        }
    }
}
注意:逆向输出
2.生成新数组(将oldArr中的0项去掉,变为newArr)
public class 习题4.2 {
    public static void main(String args[]) {
        int[] oldArr = { 1, 3, 4, 5, 0, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
        int[] newArr = new int[100];
        int i;
        int c = 0;
        for (i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                newArr[c++] = oldArr[i];
            }
        }
        for (i = 0; i < c; i++) {
            System.out.printf("%d ", newArr[i]);
        }
    }
}
运行结果:
1 3 4 5 6 6 5 4 7 6 7 5
注意:设置条件使不为0的项存入新数组
3.数组合并
public class 数组合并 {
    public static void main(String args[]) {
        int[] arr1 = { 1, 7, 9, 11, 13, 15, 17, 19 };
        int[] arr2 = { 2, 4, 6, 8, 10 };
        int[] arr3 = new int[arr1.length + arr2.length];
        //新数组长度:两个数组长度之和
        int i,cnt = 0;
        for (i = 0; i < arr1.length; i++) {
            arr3[cnt++] = arr1[i];
        }
        for (i = 0; i < arr2.length; i++) {
            arr3[cnt++] = arr2[i];
        }
        for (i = 0; i < arr3.length; i++) {
            System.out.printf("%d ", arr3[i]);
        }
    }
}
运行结果:
1 7 9 11 13 15 17 19 2 4 6 8 10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值