JavaSE练习题(上)

目录

 第一章:

第一题

第二题

第三题

第四题

第五题

第六题

第七题

第八题

第九题

第十题

简答题

第二章

第一题

第二题

第三题

第四题

第五题

第六题

第七题

第八题

第九题

第十题

阅读代码题:

第一题

第二题

第三题

第四题

第三章

第一题

第二题

第三题

第四题

第五题

第六题

第七题

简答题

第一题

第四章

第一题

第二题

第三题

第四题

第五题

第六题

第七题

第八题

第九题

第五章

一维数组基础题目

第一题:需求实现

第二题:需求实现

第三题:需求实现

第四题:需求实现

第五题:需求实现

第六题:需求实现

第七题:需求实现

第八题:需求实现

第九题:

第十题:

第十一题:

第十二题:

二维数组基础题目

第一题:

第二题:

第三题:


 第一章:

第一题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test1

  2. 定义 main方法

  3. 控制台输出5行字符串类型常量值

  4. 控制台输出5行字符类型常量值

class Test1{
    public static void main(String[] args){
        System.out.println("善学如春起之苗");
        System.out.println("不见其增,日有所长");
        System.out.println("假学如磨刀之石");
        System.out.println("不见其损,年有所亏");
        System.out.println("加油吧!少年");
        
        System.out.println("J");
        System.out.println("A");
        System.out.println("V");
        System.out.println("A");
        System.out.println("!");
    }
}

第二题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test2

  2. 定义 main方法

  3. 控制台输出5行整数类型常量值

  4. 控制台输出5行小数类型常量值

public class Test2 {
​
    public static void main(String[] args) {
        System.out.println(-2147483648);
        System.out.println(-100);
        System.out.println(0);
        System.out.println(100);
        System.out.println(2147483647);
        System.out.println(-100.0);
        System.out.println(-10.0);
        System.out.println(0.0);
        System.out.println(10.9);
        System.out.println(100.9);
    }
​
}

第三题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test3

  2. 定义 main方法

  3. 控制台输出所有布尔类型常量值

public class Test3 {
    public static void main(String[] args) {
        System.out.println(true);
        System.out.println(false);
    }
}

第四题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test4

  2. 定义 main方法

  3. 定义2个 byte类型变量,分别赋byte类型范围内最大值和最小值,并输出在控制台.

  4. 定义2个 short类型变量,分别赋short类型范围内的值,并输出在控制台.

  5. 定义2个 int类型变量,分别赋int类型范围内的值,并输出在控制台.

  6. 定义2个 long类型变量,分别赋超过int类型范围的值,并输出在控制台.

public class Test4 {
​
    public static void main(String[] args) {
        byte minByte = -128;
        byte maxByte = 127;
        System.out.println(minByte);
        System.out.println(maxByte);
        
        short minShort = -32768;
        short maxShort = 32767;
        System.out.println(minShort);
        System.out.println(maxShort);
        
        int minInt = -2147483648;
        int maxInt = 2147483647;
        System.out.println(minInt);
        System.out.println(maxInt);
        
        long minLong = -2147483649L;
        long maxLong = 2147483648L;
        System.out.println(minLong);
        System.out.println(maxLong);
    }
​
}

第五题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test5

  2. 定义 main方法

  3. 定义2个 float类型变量,分别赋值,并输出在控制台.

  4. 定义2个 double类型变量,分别赋值,并输出在控制台.

public class Test5 {
​
    public static void main(String[] args) {
        float f1 = -3.14F;
        float f2 = 3.14F;
        System.out.println(f1);
        System.out.println(f2);
        
        double d1 = -3.4;
        double d2 = 3.4;
        System.out.println(d1);
        System.out.println(d2);
    }
​
}

第六题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test6

  2. 定义 main方法

  3. 定义5个 char类型变量,分别赋值,并输出在控制台.

  4. 定义2个 boolean类型变量,分别赋值,并输出在控制台.

public class Test6 {
    public static void main(String[] args) {
        char c1 = '9';
        //或 char c1 = 57;
        char c2 = 'J';
        char c3 = 'a';
        char c4 = ' ';
        char c5 = '@';
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
        System.out.println(c4);
        System.out.println(c5);
        
        boolean yes = true;
        boolean no = false;
        System.out.println(yes);
        System.out.println(no);
    }
}

第七题

  • 按步骤编写代码,效果如图所示:

  • 步骤图解:

  • 开发提示:定义变量不赋值的格式

    //  数据类型 变量名 ;
        int temp;
  • 编写步骤:

  1. 定义类 Test7

  2. 定义 main方法

  3. 定义两个整数变量a,b并赋值

  4. 控制台输出变量a,b互换前的值

  5. 定义一个第三方变量temp,使a,b的值互换

  6. 控制台输出变量a,b互换后的值

public class Test7 {
​
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println("互换前:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        
        int temp = a;
        a = b;
        b = temp;
        System.out.println("互换后:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
​
}

第八题

  • 按步骤编写代码,效果如图所示:

  • 开发提示:四则运算的符号

    加: +
    减: -
    乘: *
    除: /
  • 编写步骤:

  1. 定义类 Test8

  2. 定义 main方法

  3. 定义2个int类型变量x、y,x赋值为100,y赋值为200

  4. 定义新变量add,保存变量x,y的和并打印到控制台

  5. 定义新变量sub,保存变量x,y的差并打印到控制台

  6. 定义新变量mul,保存变量x,y的积并打印到控制台

  7. 定义新变量div,保存变量x,y的商并打印到控制台

public class Test8 {

	public static void main(String[] args) {
		int x = 100;
		int y = 200;
		System.out.println("x,y的和为:" + (x+y));
		System.out.println("x,y的差为:" + (x-y));
		System.out.println("x,y的积为:" + (x*y));
		System.out.println("x,y的商为:" + (x/y));
	}

}

第九题

  • 按步骤编写代码,效果如图所示:

  • 开发提示:观察小数类型数值运算后的结果.

    小数运算经常出现精度丢失的问题,不建议使用基本类型运算.
  • 编写步骤:

  1. 定义类 Test9

  2. 定义 main方法

  3. 定义2个double类型变量x、y,x赋值为100.8,y赋值为20.6

  4. 定义新变量add,保存变量x,y的和并打印到控制台

  5. 定义新变量sub,保存变量x,y的差并打印到控制台

  6. 定义新变量mul,保存变量x,y的积并打印到控制台

  7. 定义新变量div,保存变量x,y的商并打印到控制台

  • 提示:

  1. 加法:+

  2. 减法:-

  3. 乘法:*

  4. 除法:/

public class Test9 {

	public static void main(String[] args) {
		double x = 100.8;
		double y = 20.6;
		
		double add = x + y;
		System.out.println("x,y的和为:" + add);
		
		double sub = x - y;
		System.out.println("x,y的差为:" + sub);
		
		double mul = x * y;
		System.out.println("x,y的积为:" + mul);
		
		double div = x / y;
		System.out.println("x,y的商为:" + div);
	}

}

第十题

  • 按步骤编写代码,效果如图所示:

  • 开发提示:不换行的输出

    System.out.print("整数类型-byte:"); // 去掉ln ,输出内容后,没有换行
    System.out.println(10);// 带有ln,输出内容后,带有换行
  • 编写步骤:

  1. 定义类 Test10

  2. 定义 main方法

  3. 定义byte类型变量,并赋值为10,不换行输出类型说明,换行输出变量值。

  4. 定义short类型变量,并赋值为100,不换行输出类型说明,换行输出变量值。

  5. 定义int类型变量,并赋值为1000,不换行输出类型说明,换行输出变量值。

  6. 定义long类型变量,并赋值为10000,不换行输出类型说明,换行输出变量值。

  7. 定义float类型变量,并赋值为100000.0,不换行输出类型说明,换行输出变量值。

  8. 定义double类型变量,并赋值为1000000.0,不换行输出类型说明,换行输出变量值。

  9. 定义char类型变量,并赋值为'Z',不换行输出类型说明,换行输出变量值。

  10. 定义boolean类型变量,并赋值为false,不换行输出类型说明,换行输出变量值。

public class Test10 {
	public static void main(String[] args) {
		byte b = 10;
		System.out.print("整数类型-byte:");
		System.out.println(b);
		
		short s = 100;
		System.out.print("整数类型-short:");
		System.out.println(s);
		
		int i = 1000;
		System.out.print("整数类型-int:");
		System.out.println(i);
		
		long l = 10000L;
		System.out.print("整数类型-long:");
		System.out.println(l);
		
		float f = 100000.0F;
		System.out.print("小数类型-float:");
		System.out.println(f);
		
		double d = 1000000.0;
		System.out.print("小数类型-double:");
		System.out.println(d);
		
		char c = 'Z';
		System.out.print("字符类型-char:");
		System.out.println(c);
		
		boolean no = false;
		System.out.print("布尔类型-boolean:");
		System.out.println(no);
	}
}

简答题

1、Java的基本数据类型有哪些?String是基本数据类型吗?

Java的基本数据类型有:byte,short,int,long,float,double,char,boolean
String不是基本数据类型

2、float f=3.4;是否正确,表达式15/2*2的值是多少

float f=3.4; //错误,因为3.4默认是double类型
System.out.println(15/2*2); //14,因为15/2结果是7

3、char型变量中是否可以存储一个汉字?

可以

4、如何用最有效的的方法计算2乘以8

2<<3

第二章

第一题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test1

  2. 定义 main方法

  3. 定义两个byte类型变量b1,b2,并分别赋值为10和20.

  4. 定义变量b3,保存b1和b2的和,并输出.

  5. 定义两个short类型变量s1,s2,并分别赋值为1000和2000.

  6. 定义变量s3,保存s1和s2的和,并输出.

  7. 定义一个char类型变量ch1赋值为'a',一个int类型变量i1赋值为30.

  8. 定义变量ch3,保存c1和i1的差,并输出.

public class Test01 {
​
    public static void main(String[] args) {
        byte b1 = 10;
        byte b2 = 20;
        byte b3 = (byte)(b1 + b2);
        System.out.println("byte类型的b1和b2的和为:");
        System.out.println(b3);
        
        short s1 = 1000;
        short s2 = 2000;
        short s3 = (short)(s1 + s2);
        System.out.println("short类型的s1和s2的和为:");
        System.out.println(s3);
        
        char c1 = 'a';
        int i1 = 30;
        int ch3 = c1 - i1;
        System.out.println("char类型的ch1和int类型的i1的差:");
        System.out.println(ch3);
    }
}

第二题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test2

  2. 定义 main方法

  3. 定义 int类型变量i1 和 long类型变量l1

  4. 定义变量add,保存i1和l1的和,并输出.

  5. 定义 long类型变量l2 和 float类型变量f2

  6. 定义变量add2,保存l2和f2的和,并输出.

  7. 定义 int类型变量i3 和 double类型变量d3

  8. 定义变量add3,保存i3和d3的和,并输出.

  9. 定义 float类型变量f4 和 double类型变量d4

  10. 定义变量add4,保存f4和d4的和,并输出.

public class Test2 {
    public static void main(String[] args) {
        int i1 = 100;
        long l1 = 200;
        long add = i1 + l1;
        System.out.println("add的值:" + add);
        
        long l2 = 1000000L;
        float f2 = 0.44F;
        float add2 = l2 + f2;
        System.out.println("add2的值:" + add2);
        
        int i3 = 100000;
        double d3 = 0.45;
        double add3 = i3 + d3;
        System.out.println("add3的值:" + add3);
        
        float f4 = 1000000;
        double d4 = 1.2625;
        double add4 = f4 + d4;
        System.out.println("add4的值:" + add4);
    }
}
​

第三题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test3

  2. 定义 main方法

  3. 定义char类型变量ch,赋值为'J'

  4. 使用强制转换的方式,将变量ch转换为小写'j',并输出

  5. 定义char类型变量ch2,赋值为'a'

  6. 使用+=的方式,将变量ch2转换为大写'A',并输出

  7. 定义double类型变量d3,int类型变量i3

  8. 定义double变量sum3,保存d3与i3的和,输出sum3的值和sum3去除小数部分的值

  9. 定义double类型变量d4,int类型变量i4

  10. 定义int变量mul4,保存d4和i4乘积的整数部分,并输出

public class Test03 {
​
    public static void main(String[] args){
        char ch = 'J';
        char ch1 = (char)(ch + 32);
        System.out.println(ch1);
        
        char ch2 = 'a';
        ch2 -= 32;
        System.out.println(ch2);
        
        double d3 = 100.5;
        int i3 = 3;
        double sum3 = d3 + i3;
        int sum4 = (int)sum3;
        System.out.println("sum3的值:" + sum3);
        System.out.println("sum3的整数部分的值:" + sum4);
        
        double d4 = 4.0;
        int i4 = 435;
        int mul4 = (int)(d4 * i4);
        System.out.println("mul4的整数部分的值:" + mul4);
    }
​
}

第四题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test4

  2. 定义 main方法

  3. 定义两个int类型变量a1和a2,分别赋值10,11,判断变量是否为偶数,拼接输出结果

  4. 定义两个int类型变量a3和a4,分别赋值12,13,判断变量是否为奇数,拼接输出结果

public class Test04 {
    public static void main(String[] args) {
        int a1 = 10;
        int a2 = 11;
        int a3 = 12;
        int a4 = 13;
        System.out.println("10是偶数?" + (a1 % 2 == 0));
        System.out.println("11是偶数?" + (a2 % 2 == 0));
        System.out.println("12是奇数?" + (a3 % 2 != 0));
        System.out.println("13是奇数?" + (a4 % 2 != 0));
    }
}

第五题

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test5

  2. 定义 main方法

  3. 定义一个int类型变量a,变量b,都赋值为20.

  4. 定义boolean类型变量bo , 判断++a 是否被3整除,并且a++ 是否被7整除,将结果赋值给bo

  5. 输出a的值,bo的值.

  6. 定义boolean类型变量bo2 , 判断b++ 是否被3整除,并且++b 是否被7整除,将结果赋值给bo2

  7. 输出b的值,bo2的值.

public class Test05 {
    public static void main(String[] args){
        int a = 20;
        int b = 20;
        boolean bo = ((++a % 3) == 0) && ((a++ % 7) == 0);
        boolean bo2 = ((b++ % 3) == 0) && ((++b % 7) == 0);
        
        System.out.println("bo的值:" + bo);
        System.out.println("a的值:" + a);
        System.out.println("----------------------------");
        System.out.println("bo2的值:" + bo2);
        System.out.println("b的值:" + b);
    }
}

第六题

案例:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时?

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类Test6

  2. 定义main方法

  3. 定义一个int类型变量hours,赋值为89

  4. 定义一个int类型变量day,用来保存89小时中天数的结果

  5. 定义一个int类型变量hour,用来保存89小时中不够一天的剩余小时数的结果

  6. 输出结果

public class Test06 {
    public static void main(String[] args){
        int hours = 89;
        int day = hours / 24;
        int hour = hours % 24;
        System.out.println("为抵抗洪水,战士连续作战89小时:");
        System.out.println(hours + "是" + day + "天" + hour +"小时");
    }
}

第七题

案例:今天是周2,100天以后是周几?

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类Test7

  2. 定义main方法

  3. 定义一个int类型变量week,赋值为2

  4. 修改week的值,在原值基础上加上100

  5. 因为week的值加上100后超过了星期的范围,重写修改week的值

  6. 输出结果,在输出结果的时候考虑特殊值,例如周日

public class Test07 {
​
    public static void main(String[] args){
        int week = 2;
        week += 100;
        week %= 7;
        System.out.println("今天是周2,100天以后是周" + (week==0?"日":week));
    }
​
}

第八题

案例:求三个整数x,y,z中的最大值

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类Test8

  2. 定义main方法

  3. 定义三个int类型变量,x,y,z,随意赋值整数值

  4. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符)

  5. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符)

  6. 输出结果

public class Test08 {
    public static void main(String[] args) {
        int x = 3;
        int y = 4;
        int z = 1;
        int max = x > y ? x : y;
        max = max > z ? max : z;
        System.out.println(x + "," + y + "," + z + "中的最大值是:" + max);
    }
}

第九题

案例:给定一个年份,判断是否是闰年

闰年的判断标准是:

1)可以被4整除,但不可被100整除

2)可以被400整除

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类Test9

  2. 定义main方法

  3. 定义一个int类型变量year,随意赋值一个年份

  4. 定一个一个boolean类型变量,用来保存这个年份是否是闰年的结果

  5. 输出结果

public class Test09 {

	public static void main(String[] args) {
		int year = 2018;
		boolean result = year%4==0 && year%100!=0 || year%400==0;
		System.out.println(year + (result ? "是闰年" : "不是闰年"));
	}

}

第十题

案例:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。它需要一个程序将华氏温度(80度)转换为摄氏度,并以华氏度和摄氏度为单位分别显示该温度。

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类Test10

  2. 定义main方法

  3. 定义一个double类型变量hua,存储华氏温度80

  4. 定义一个double类型变量she,存储摄氏温度,根据公式求值

  5. 输出结果

public class Test10 {
	public static void main(String[] args) {
		double hua = 80;
		double she = (hua-32)/1.8;
		System.out.println("华氏度" + hua+"℉转为摄氏度是" +she+"℃");
	}
}

阅读代码题:

第一题

如下代码的计算结果是:
int i = 1;
i *= 0.2;  
i++;
System.out.println("i=" + i);//i=1

第二题

如下代码的运算结果是:
int i = 2;
i *= i++;

int j = 2;
j *= j+1; 

int k = 2;
k *= ++k;

System.out.println("i=" + i);//i=4
System.out.println("j=" + j);//i=6
System.out.println("k=" + k);//i=6

第三题

public static void main(String[] args) {
		int a = 3;
		int b = 1;
		if(a = b){//编译报错
			System.out.println("Equal");
		}else{
			System.out.println("Not Equal");
		}
	}

第四题

public static void main(String[] args) {
		int a = 8, b = 3;
		System.out.println(a>>>b);//1
		System.out.println(a>>>b | 2);//2
	}

第三章

第一题

语法点:变量,运算符,if...else

案例:从键盘输入一个整数,判断它是奇数还是偶数(这里把0归为偶数)

开发提示:

键盘输入需要用到Scanner类。

java.util.Scanner input = new java.util.Scanner(System.in);//准备从键盘输入的扫描仪
int num = input.nextInt();//输入整数

能够被2整除的是偶数,不能被2整除的是奇数

public class Test01{
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("请输入一个整数:");
        int num = input.nextInt();
        if(num % 2 == 0){
            System.out.println(num + "是偶数");
        }else{
            System.out.println(num + "是奇数");
        }
    }
}

第二题

语法点:变量,运算符,if...else

案例:从键盘输入一个字符,判断它是字母还是数字,还是其他字符

开发提示:

键盘输入需要用到Scanner类。

java.util.Scanner input = new java.util.Scanner(System.in);//准备接收从键盘输入的扫描仪
char c = input.next().charAt(0);//输入单个字符

数字范围:'0'-'9'

字母范围:'A'-'Z','a'-'z'

public class Test02{
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("请输入一个字符:");
        char c = input.next().charAt(0);
        if(c >= '0' && c <= '9'){
            System.out.println(c + "是数字.");
        }else if(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'){
            System.out.println(c + "是字母.");
        }else{
            System.out.println(c + "非数字非字母的其他字符.");
        }
    }
}

第三题

  • 语法点:变量,运算符,if...else

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test2

  2. 定义 main方法

  3. 定义变量折扣 discount,初始化为1, 总价totalPrice的值从键盘输入

  4. 判断当totalPrice >=500 ,discount赋值为0.5

  5. 判断当totalPrice >=400<500时,discount赋值为0.6

  6. 判断当totalPrice >=300<400时,discount赋值为0.7

  7. 判断当totalPrice >=200<300时,discount赋值为0.8

  8. 判断当totalPrice >=0<200时,discount赋值为1

  9. 判断当totalPrice<0时,显示输入有误

  10. 输出结果

  • 开发提示:

    键盘输入需要用到Scanner类。

    java.util.Scanner input = new java.util.Scanner(System.in);//准备接收从键盘输入的扫描仪
    double totalPrice = input.nextDouble();//输入double值
public class Test03{
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("请输入总价格:");
        double discount = 1;
        double totalPrice = input.nextDouble();
        if(totalPrice>=500){
            discount = 0.5;
        }else if(totalPrice>=400){
            discount = 0.6;
        }else if(totalPrice>=300){
            discount = 0.7;
        }else if(discount>=200){
            discount = 0.8;
        }else if(discount >= 0){
            discount = 1;
        }else{
            System.out.println("输入有误!");
        }
        System.out.println("总价:" + totalPrice);
        System.out.println("折扣:" + discount);
        System.out.println("折扣后总价:" + totalPrice*discount);
    }
}

第四题

语法点:变量,运算符,if...else

案例:从键盘输入生日,判断星座

  • 开发提示:

    1. 各个星座的日期范围如下:

public class Test04 {
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("请输入月份:");
        int month = input.nextInt();
​
        System.out.print("请输入日期:");
        int day = input.nextInt();
​
        if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
            System.out.println("生日" + month + "月" + day + "日是水瓶座");
        } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
            System.out.println("生日" + month + "月" + day + "日是双鱼座");
        }else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
            System.out.println("生日" + month + "月" + day + "日是白羊座");
        }else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
            System.out.println("生日" + month + "月" + day + "日是金牛座");
        }else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
            System.out.println("生日" + month + "月" + day + "日是双子座");
        }else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) {
            System.out.println("生日" + month + "月" + day + "日是巨蟹座");
        }else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
            System.out.println("生日" + month + "月" + day + "日是狮子座");
        }else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
            System.out.println("生日" + month + "月" + day + "日是处女座");
        }else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
            System.out.println("生日" + month + "月" + day + "日是天平座");
        }else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) {
            System.out.println("生日" + month + "月" + day + "日是天蝎座");
        }else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) {
            System.out.println("生日" + month + "月" + day + "日是射手座");
        }else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {
            System.out.println("生日" + month + "月" + day + "日是摩羯座");
        }
    }
}

第五题

语法点:变量,运算符,switch...case

案例需求:编写一个程序,为一个给定的年份找出其对应的中国生肖。中国的生肖基于12年一个周期,每年用一个动物代表:rat(鼠)、ox(牛)、tiger(虎)、rabbit(兔)、dragon(龙)、snake(蛇)、

horse(马)、sheep(羊)、monkey(候)、rooster(鸡)、dog(狗)、pig(猪)。

提示:2017年:鸡 2017 % 12 == 1

public class Test05 {
​
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("请输入年份:");
        int year = input.nextInt();
        
        switch(year%12){
        case 1:
            System.out.println("鸡");
            break;
        case 2:
            System.out.println("狗");
            break;
        case 3:
            System.out.println("猪");
            break;
        case 4:
            System.out.println("鼠");
            break;
        case 5:
            System.out.println("牛");
            break;
        case 6:
            System.out.println("虎");
            break;
        case 7:
            System.out.println("兔");
            break;
        case 8:
            System.out.println("龙");
            break;
        case 9:
            System.out.println("蛇");
            break;
        case 10:
            System.out.println("马");
            break;
        case 11:
            System.out.println("羊");
            break;
        case 0:
            System.out.println("猴");
            break;
        }
    }
​
}

第六题

语法点:变量,运算符,if..else

案例:

  • 开发提示:

  1. Math.sqrt(num):求num的平方根

  2. 定义double类型变量a,b,c,并从键盘输入它们的值

    键盘输入需要用到Scanner类。

java.util.Scanner input = new java.util.Scanner(System.in);//准备接收从键盘输入的扫描仪
double totalPrice = input.nextDouble();//输入double值
public class Test06{
	public static void main(String[] args){
		java.util.Scanner input = new java.util.Scanner(System.in);
		
		System.out.print("请输入方程的参数a:");
		double a = input.nextDouble();
		
		System.out.print("请输入方程的参数b:");
		double b = input.nextDouble();
		
		System.out.print("请输入方程的参数c:");
		double c = input.nextDouble();
		
		if(a!=0){
			double d = b*b - 4*a*c;
			if(d>0){
				double x1 = (-b + Math.sqrt(d))/(2*a);
				double x2 = (-b - Math.sqrt(d))/(2*a);
				System.out.println("两个解:x1 = " + x1 + ",x2 = " + x2);
			}else if(d==0){
				double x = -b/(2*a);
				System.out.println("一个解:x = " + x);
			}else{
				System.out.println("在实数范围内无解");
			}
		}else if(a==0 && b!=0){
			double x = -c/b;
			System.out.println("一个解:x = " + x);
		}else{
			System.out.println("不是方程");
		}
	}
}

第七题

语法点:变量,运算符,if和switch...case

案例:已知2019年1月1日是星期二,从键盘输入2019年的任意一天,请判断它是星期几

  • 开发提示:

  1. 先统计这一天是这一年的第几天days

  2. 然后声明一个变量week,初始化为2

  3. 然后week加上days-1

  4. 然后求week与7的模数

  5. 然后输出结果,考虑星期天的特殊判断

public class Test07 {
	public static void main(String[] args){
		//1、从键盘分别输入年、月、日
		java.util.Scanner input = new java.util.Scanner(System.in);
		
		System.out.print("月:");
		int month = input.nextInt();
		
		System.out.print("日:");
		int day = input.nextInt();
		
		//判断这一天是当年的第几天==>从1月1日开始,累加到xx月xx日这一天
		//(1)[1,month-1]个月满月天数
		//(2)第month个月的day天
		//(3)单独考虑2月份是否是29天(依据是看year是否是闰年)
		
		//2、声明一个变量days,用来存储总天数
		//int days = 0;
		//累加第month个月的day天
		//days += day;
		
		//修改上面的代码,直接把days初始化为day
		int days = day;
		
		//3、累加[1,month-1]个月满月天数
		switch(month){
			case 12:
				//累加的1-11月
				days += 30;//这个30是代表11月份的满月天数
				//这里没有break,继续往下走
			case 11:
				//累加的1-10月
				days += 31;//这个31是代表10月的满月天数
				//这里没有break,继续往下走
			case 10:
				days += 30;//9月
			case 9:
				days += 31;//8月
			case 8:
				days += 31;//7月
			case 7:
				days += 30;//6月
			case 6:
				days += 31;//5月
			case 5:
				days += 30;//4月
			case 4:
				days += 31;//3月
			case 3:
				days += 28;//2月,因为2019年的2月是28天
			case 2:
				days += 31;//1月
		}
		
		//days 里面存的是这一天是这一年的第几天
		//已知2019年1月1日是星期二
		//假设我输入的就是1月1日,那么days中就是1
		int week = 1;//2018年12月31日的星期
		week += days;
		week %= 7;
		System.out.print(month+"月" + day +"日是这一年的第"+days+"天,是星期" + (week==0?"天":week)) ;
		
	}
}	

简答题

第一题

1、switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?

可以作用在byte上,
不能作用在long上
可以作用在String上

第四章

第一题

  • 语法点:运算符,while,if

  • 按步骤编写代码,效果如图所示:

  • 编写步骤:

  1. 定义类 Test1

  2. 定义 main方法

  3. 定义变量i为0,i2为10

  4. 使用第一个while循环,当条件为i小于5 时,则进入循环

  5. 循环内,i自增,i2自增

  6. 循环内,使用if判断,当i大于等于 2 并且i2小于15 时,同时输出i和i2的值

  7. 使用第二个while循环,当条件为i2小于20 时,则进入循环

  8. 循环内,i自增,i2自增

  9. 循环内,使用if判断,当i大于8 或者i2小于等于18 时,同时输出i和i2的值

public class Test01 {
    public static void main(String[] args) {
        int i = 0;
        int i2 = 10;
        while(i<5){
            i++;
            i2++;
            if(i>=2 && i2<15){
                System.out.println("i:" + i + ",i2:" + i2);
            }
        }
        System.out.println("----------------------");
        while(i2<20){
            i++;
            i2++;
            if(i>8 || i2<=18){
                System.out.println("i:" + i + ",i2:" + i2);
            }
        }
    }
}
​

第二题

  • 语法点:方法,运算符,for,while

  • 按步骤编写代码,效果如图所示:

  • 编写步骤

  1. 定义类 Test2,定义 main方法

  2. main方法中,定义int类型变量 a为1

  3. 使用while循环,判断a<=5,进入循环.

  4. while循环内部,使用for循环,初始化int类型变量b为1,当b<=5时进入循环, 步进表达式b++

  5. for循环内,不换行输出i并拼接" "

  6. for循环外,输出换行.

  7. for循环外,a自增.

public class Test02 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println("--------------------------");
        while(a<=5){
            for (int b = 1; b <= 5; b++) {
                System.out.print(b+" ");
            }
            System.out.println();
            a++;
        }
        System.out.println("--------------------------");
    }
}
​

第三题

  • 语法点:运算符,for,if

  • 按步骤编写代码,效果如图所示:

  • 编写步骤

  1. 定义类 Test5

  2. 定义 main方法

  3. 定义变量jj为20,a为0,b为0

  4. 使用for循环,初始化值ii为0,当ii<jj 时进入循环,步进表达式为ii+=2,jj自减

  5. 循环内,使用if判断ii被3整除,ii赋值给a,并输出ii拼接"Hello"

  6. 不被3整除,ii赋值给b,并输出ii拼接"World"

  7. 循环外,按照格式,打印a与b的乘积

public class Test03 {
    public static void main(String[] args) {
        int jj = 20;
        int a = 0;
        int b = 0;
        for(int ii = 0;ii < jj;ii+=2,jj--){
            if(ii % 3 == 0){
                a = ii;
                System.out.println(ii + " Hello");
            }else{
                b = ii;
                System.out.println(ii + "  World");
            }
        }
        System.out.println("a*b=" + a + "*" + b + "=" + a*b); 
    }
}

第四题

  • 语法点:运算符,for,switch

  • 打印星座信息,效果如图所示:

开发提示: 

  1. 1-12的规律数字,可以使用for循环处理

  2. 根据不同的数字,匹配显示不同的文字,可以使用switch匹配

public class Test04 {
    public static void main(String[] args) {
        for (int i = 1; i <= 12; i++) {
            switch(i){
            case 1:
                System.out.println( i + ":水瓶座");
                break;
            case 2:
                System.out.println( i + ":双鱼座");
                break;
            case 3:
                System.out.println( i + ":白羊座");
                break;
            case 4:
                System.out.println( i + ":金牛座");
                break;
            case 5:
                System.out.println( i + ":双子座");
                break;
            case 6:
                System.out.println( i + ":巨蟹座");
                break;
            case 7:
                System.out.println( i + ":狮子座");
                break;
            case 8:
                System.out.println( i + ":处女座");
                break;
            case 9:
                System.out.println( i + ":天平座");
                break;
            case 10:
                System.out.println( i + ":天蝎座");
                break;
            case 11:
                System.out.println( i + ":射手座");
                break;
            case 12:
                System.out.println( i + ":摩羯座");
                break;
            }
        }
    }
}
​

第五题

语法点:运算符,for,if

案例需求:从键盘分别输入年、月、日,使用循环for+if实现,判断这一天是当年的第几天

注:判断一年是否是闰年的标准:

1)可以被4整除,但不可被100整除

2)可以被400整除

  • 开发提示:

    1. 循环1-month月

    2. 在循环中判断该月是31天、30天、28/29天,分别累加

public class Test05 {
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);
​
        System.out.print("请输入年:");
        int year = input.nextInt();
​
        System.out.print("请输入月:");
        int month = input.nextInt();
​
        System.out.print("请输入日:");
        int day = input.nextInt();
​
        int days = day;
        for (int i = 1; i < month; i++) {
            if (i == 4 || i == 6 || i == 9 || i == 11) {
                days += 30;
            } else if (i == 2) {
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    days += 29;
                } else {
                    days += 28;
                }
            } else {
                days += 31;
            }
        }
​
        System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + days + "天");
    }
}

第六题

案例需求:假设从2000年1月1日开始三天打鱼,两天晒网,从键盘输入今天的日期年、月、日,显示今天是打鱼还是晒网?

public class Test06 {
    public static void main(String[] args){
        //1、从键盘分别输入年、月、日
        java.util.Scanner input = new java.util.Scanner(System.in);
        
        System.out.print("年:");
        int year = input.nextInt();
        
        System.out.print("月:");
        int month = input.nextInt();
        
        System.out.print("日:");
        int day = input.nextInt();
        
        /*
        (1)先算出,这一天距离2000年1月1日是第几天
        ①第month月的day天
        ②第year年的[1,month-1]的满月
        ③从[2000,year-1]的满年天数
        (2)用总天数%5,看余数,余数是1,2,3是打鱼,4和0是晒网
        */
        
        int days = day;//①第month月的day天
        
        //②累加[1,month-1]的满月天数
        for(int i=1; i<month; i++){//这个i代表月份
            if(i==4 || i==6 || i==9 || i==11){
                days+=30;
            }else if(i==2){
                if(year%4==0 && year%100!=0 || year%400==0){
                    days+=29;
                }else{
                    days+=28;
                }
            }else{
                days+=31;
            }
        }
        
        //③从[2000,year-1]的满年天数
        for(int i=2000; i<year; i++){//这个i代表年份
            if(i%4==0 && i%100!=0 || i%400==0){
                days+=366;
            }else{
                days+=365;
            }
        }
        
        //判断
        if(days%5==1 || days%5==2 || days%5==3){
            System.out.println("打鱼");
        }else{
            System.out.println("晒网");
        }
    }
}
 

第七题

  • 打印倒三角形,效果如图所示:

  • 开发提示:
  1. 平面图形涉及到有行有列,考虑到嵌套for循环

  2. 一个外循环控制行,两个内循环控制输出内容

  3. 一个内循环负责输出空格,另一个内循环输出"*"

public class Test07 {
    public static void main(String[] args){
        for(int i=1; i<=5; i++){//控制行数
            /*
            每一行有三件事:
            (1)打印n个空格
            (2)打印m个" *"
            (3)换行
            */
            //(1)打印n个空格
            /*
            第1行:0个,当i=1, j应该让它第一次循环都不满足 j<i
            第2行:1个,当i=2, j运行1次,j=1
            第3行:2个,当i=3, j运行2次,j=1,2
            第4行:3个,当i=4, j运行3次,j=1,2,3
            第5行:4个,当i=5, j运行4次,j=1,2,3,4
            */
            for(int j=1; j<i; j++){
                System.out.print(" ");
            }
            //(2)打印m个" *"
            /*
            第1行:5个,当i=1,j运行5次,j=1,2,3,4,5       j<=6-i
            第2行:4个,当i=2,j运行4次,j=1,2,3,4
            第3行:3个,当i=3,j运行3次,j=1,2,3
            第4行:2个,当i=4,j运行2次,j=1,2
            第5行:1个,当i=5,j运行1次,j=1
            */
            for(int j=1; j<=6-i; j++){
                System.out.print(" *");
            }
            
            
            //(3)换行
            System.out.println();
        }
    }
}
​
class Day04_Test07_2{
    public static void main(String[] args){
        for(int i=1; i<=5; i++){//控制行数
            /*
            每一行有三件事:
            (1)打印n个空格
            (2)打印m个" *"
            (3)换行
            */
            //(1)打印n个空格
            /*
            第1行:0个,当i=1, j应该让它第一次循环都不满足 j<i
            第2行:1个,当i=2, j运行1次,j=1
            第3行:2个,当i=3, j运行2次,j=1,2
            第4行:3个,当i=4, j运行3次,j=1,2,3
            第5行:4个,当i=5, j运行4次,j=1,2,3,4
            */
            for(int j=1; j<i; j++){
                System.out.print(" ");
            }
            //(2)打印m个" *"
            /*
            第1行:5个,当i=1,j运行5次,j=5,4,3,2,1  j>=i
            第2行:4个,当i=2,j运行4次,j=5,4,3,2
            第3行:3个,当i=3,j运行3次,j=5,4,3
            第4行:2个,当i=4,j运行2次,j=5,4
            第5行:1个,当i=5,j运行1次,j=5
            */
            for(int j=5; j>=i; j--){
                System.out.print(" *");
            }
            
            
            //(3)换行
            System.out.println();
        }
    }
}

第八题

  • 打印『X』对称图形,效果如图所示:

  • 开发提示:

  1. 平面图形涉及到有行有列,考虑到嵌套for循环

  2. 一个外循环控制行,一个内循环控制输出内容

  3. 在内循环中,根据变量的变化规律,判断输出"O"还是"*"

public class Test08 {
	public static void main(String[] args){
		for(int i=1; i<=7; i++){//控制行数
			//(1)打印该行的*或o
			/*
			发现O+*的总个数是7个
			当i=1, 当j=1和j=7的时候是O,其余的是* 
			当i=2, 当j=2和j=6的时候是O,其余的是* 
			当i=3, 当j=3和j=5的时候是O,其余的是* 
			当i=4, 当j=4的时候是O,其余的是* 
			当i=5, 当j=5和j=3的时候是O,其余的是* 
			当i=6, 当j=6和j=2的时候是O,其余的是* 
			当i=7, 当j=7和j=1的时候是O,其余的是* 
			*/
			for(int j=1; j<=7; j++){
				if(i==j || i==8-j){
					System.out.print("O");
				}else{
					System.out.print("*");
				}
			}
			
			//(2)每一行的最后一件事是换行
			System.out.println();
		}
	}
}

第九题

  • 打印菱形

  • 开发提示:

  1. 平面图形涉及到有行有列,考虑到嵌套for循环

  2. 一个外循环控制行,两个内循环控制输出内容

  3. 一个内循环负责输出空格,另一个内循环输出"*"

public class Test09 {
	public static void main(String[] args){
		//上半部分:正的等腰三角形
		//5行
		for(int i=1; i<=5; i++){
			//(1)打印空格
			/*
			当i=1,打印4个空格,j=4,3,2,1	j>=i
			当i=2,打印3个空格,j=4,3,2
			当i=3,打印2个空格,j=4,3
			当i=4,打印1个空格,j=4
			当i=5,打印0个空格,j=4,让循环条件一次都不满足
			*/
			for(int j=4; j>=i; j--){
				System.out.print("  ");
			}
			//(2)打印*
			/*
			当i=1,打印1个,j=1					j<=2*i-1
			当i=2,打印3个,j=1,2,3,
			当i=3,打印5个,j=1,2,3,4,5
			当i=4,打印7个,j=1,2,3,4,5,6,7
			当i=5,打印9个,j=1,2,3,4,5,6,7,8,9
			*/
			for(int j=1; j<=2*i-1; j++){
				System.out.print("* ");
			}
			//(3)换行
			System.out.println();
		}
		
		
		//下半部分:倒立的等腰三角形
		//4行
		for(int i=1; i<=4; i++){
			//(1)打印空格
			/*
			当i=1,1个空格,j=1		j<=i
			当i=2,2个空格,j=1,2	
			当i=3,3个空格,j=1,2,3
			当i=4,4个空格,j=1,2,3,4
			*/
			for(int j=1; j<=i; j++){
				System.out.print("  ");
			}
			//(2)打印*
			/*
			当i=1,7个*,j=1,2,3,4,5,6,7  j<=9-2*i;
			当i=2,5个*,j=1,2,3,4,5
			当i=3,3个*,j=1,2,3
			当i=4,1个*,j=1
			*/
			for(int j=1; j<=9-2*i; j++){
				System.out.print("* ");
			}
			//(3)换行
			System.out.println();
		}
	}
}

public class Test09_02 {
	public static void main(String[] args){
		//上半部分:正的等腰三角形
		//5行
		for(int i=1; i<=5; i++){
			//(1)打印空格
			/*
			当i=1,打印4个空格,j=4,3,2,1	j>=i
			当i=2,打印3个空格,j=4,3,2
			当i=3,打印2个空格,j=4,3
			当i=4,打印1个空格,j=4
			当i=5,打印0个空格,j=4,让循环条件一次都不满足
			*/
			for(int j=4; j>=i; j--){
				System.out.print("  ");
			}
			//(2)打印*
			/*
			当i=1,打印1个,j=1					j<=2*i-1
			当i=2,打印3个,j=1,2,3,
			当i=3,打印5个,j=1,2,3,4,5
			当i=4,打印7个,j=1,2,3,4,5,6,7
			当i=5,打印9个,j=1,2,3,4,5,6,7,8,9
			*/
			for(int j=1; j<=2*i-1; j++){
				//不是全部打印*
				if(j==1 || j==2*i-1){
					System.out.print("* ");
				}else{
					System.out.print("  ");
				}
			}
			//(3)换行
			System.out.println();
		}
		
		
		//下半部分:倒立的等腰三角形
		//4行
		for(int i=1; i<=4; i++){
			//(1)打印空格
			/*
			当i=1,1个空格,j=1		j<=i
			当i=2,2个空格,j=1,2	
			当i=3,3个空格,j=1,2,3
			当i=4,4个空格,j=1,2,3,4
			*/
			for(int j=1; j<=i; j++){
				System.out.print("  ");
			}
			//(2)打印*
			/*
			当i=1,7个*,j=1,2,3,4,5,6,7  j<=9-2*i;
			当i=2,5个*,j=1,2,3,4,5
			当i=3,3个*,j=1,2,3
			当i=4,1个*,j=1
			*/
			for(int j=1; j<=9-2*i; j++){
				//不是全部打印*
				if(j==1 || j==9-2*i){
					System.out.print("* ");
				}else{
					System.out.print("  ");
				}
			}
			//(3)换行
			System.out.println();
		}
	}
}

第五章

一维数组基础题目

第一题:需求实现

  • 模拟大乐透号码:

    • 一组大乐透号码由10个1-99之间的数字组成

    • 打印大乐透号码信息

  • 代码实现,效果如图所示:

  • 开发提示:

  1. 使用数组保存录入或随机产生的号码

  2. 如果使用键盘输入,需要用到java.util.Scanner

  3. 如果使用随机产生,可以使用Math.random()或java.util.Random的nextInt(bounds)

public class Test01 {
​
    public static void main(String[] args) {
        //1、声明并创建数组
        int[] arr = new int[10];
        
        //2、为元素赋值
        java.util.Random rand = new java.util.Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = rand.nextInt(99)+1;
        }
        
        //3、输出
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
​
}

第二题:需求实现

  • 打印扑克牌.

  • 代码实现,效果如图所示:

  • 开发提示:

  1. 使用两个字符串数组,分别保存花色和点数

  2. 再用一个字符串数组保存最后的扑克牌

  3. 遍历显示

public class Test02 {
    public static void main(String[] args){
        String[] hua = {"黑桃","红桃","梅花","方片"};
        String[] dian = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String[] pu = new String[hua.length*dian.length];
        for(int i=0,k=0; i<hua.length; i++){
            for(int j=0; j<dian.length; j++,k++){
                pu[k] = hua[i]+dian[j];
            }
        }
        
        for (int i = 1; i <= pu.length; i++) {
            System.out.print(pu[i-1]+" ");
            if(i%13==0){
                System.out.println();
            }
        }
    }
}

第三题:需求实现

  • 模拟在一副牌中,抽取第1张,第5张,第50张扑克牌。

  • 代码实现,效果如图所示:

public class Test03 {
    public static void main(String[] args){
        String[] hua = {"黑桃","红桃","梅花","方片"};
        String[] dian = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String[] pu = new String[hua.length*dian.length];
        for(int i=0,k=0; i<hua.length; i++){
            for(int j=0; j<dian.length; j++,k++){
                pu[k] = hua[i]+dian[j];
            }
        }
        
        System.out.println(pu[1-1] + " " + pu[5-1] +" " + pu[50-1]);
    }
}
​

第四题:需求实现

  • 统计字符

    • 字符数组:{'a','l','f','m','f','o','b','b','s','n'}

    • 统计每个字符出现的次数并打印到控制台。

  • 代码实现,部分效果如图所示

  • 开发提示:

  1. 将数字强制转换,根据ASCII码表转换为字符。

  2. 可以定义长度26的数组,每个元素,对应去保存每种字符的出现次数,比如0索引保存a的次数,1索引保存b的次数,以此类推。

public class Test04 {
    public static void main(String[] args){
        char[] arr = {'a','l','f','m','f','o','b','b','s','n'};
        
        int[] counts = new int[26];//counts数组的元素,目前默认值都是0
        
        // counts[0] 存储 'a'字母出现的次数
        // counts[1] 存储 'b'字母出现的次数
        // counts[2] 存储 'c'字母出现的次数
      
        
        //遍历arr数组,统计每一个字母出现的次数,并且把次数存储的counts数组中
        for(int i=0; i<arr.length; i++){
            //例如:arr[0]现在是'a',那么应该counts[0]++
            //arr[1]现在是'l',那么应该counts[11]++
            //找counts[下标]其中的下标与字母'a','b'等的关系
            //例如:'a' ==》counts[0]的[0]的关系        'a'-97=97-97=0
            //例如:'l' ==》counts[11]的[11]的关系      'l'-97=108-97=11
            counts[arr[i] - 97]++;
        }
        
        //遍历counts数组显示结果
        for(int i=0; i<counts.length; i++){
            if(counts[i]!=0){
                //System.out.println(字母 + "--" + 次数);
                System.out.println((char)(i+97) + "--" + counts[i]);
            }
        }
    }
}

第五题:需求实现

  • 统计高于平均分的分数有多少个。

    • 定义数组[95, 92, 75, 56, 98, 71, 80, 58, 91, 91]。

代码实现,效果如图所示:

步骤:

  1. 先求总分
  2. 求平均分
  3. 遍历数组,统计比平均分高的个数
public class Test05 {
    public static void main(String[] args) {
        int[] arr = {95, 92, 75, 56, 98, 71, 80, 58, 91, 91};
        
        //累加总分
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        //求平均分
        int avg = sum/arr.length;
        
        //统计比平均分高的人数
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] > avg){
                count++;
            }
        }
        System.out.println("高于平均分:" + avg + "的有" + count + "个");
    }
}

第六题:需求实现

  • 判断数组中的元素值是否对称.

  • 代码实现,效果如图所示:

  • 开发提示:

    • 数组中元素首尾比较。

public class Test06 {
    public static void main(String[] args){
        int[] arr = {1,2,3,4,4,3,2,1};
        
        //(1)先假设它是对称的
        boolean flag = true;
        
        //(2)遍历,查看数组的元素是否首尾对称
        //left表示左边的下标
        //right表示右边的下标
        for(int left=0,right=arr.length-1; left<right; left++,right--){
            if(arr[left] != arr[right]){
                flag = false;
                break;
            }
        }
        
        System.out.println(flag?"对称":"不对称");
    }
}
​

第七题:需求实现

  • 比较两个数组内容是否完全一致。

  • 代码实现,效果如图所示:

  • 开发提示:

    • 长度一致,内容一致,定义为完全一致。

    public class Test07 {
        public static void main(String[] args) {
            int[] arr1 = {1,2,3,4,5,6,7};
            int[] arr2 = {1,2,3,4,5,6,7};
        
            boolean flag = true;//假设一致
            if(arr1.length!=arr2.length){
                flag = false;
            }else{
                for (int i = 0; i < arr2.length; i++) {
                    if(arr1[i] != arr2[i]){
                        flag = false;
                        break;
                    }
                }
            }
            System.out.println("是否一致:" + flag);
        }
    }

第八题:需求实现

  • 根据标准答案【ADBCD】,每题2分共10分,求出每名学生最终得分。

    • 四名同学答案分别为:

      • 小尚:【DCBAD】

      • 小硅:【ADBCD】

      • 小谷:【ADBCA】

      • 小好:【ABCDD】

    • 每答对一题,得2分,输出四名同学的最终得分。

  • 代码实现,效果如图所示:

    提示:标准答案放到一个一维数组中,每个同学答案也各自放到一个一维数组中,然后分别统计得分

public class Test08 {
    public static void main(String[] args){
        //标准答案:
        char[] answer = {'A','D','B','C','D'};
        
        //学生的答案
        char[] shang = {'D','C','B','A','D'};
        char[] gui = {'A','D','B','C','D'};
        char[] gu = {'A','D','B','C','A'};
        char[] hao = {'A','B','C','D','D'};
        
        //求出每名学生最终得分。
        int shangFen = 0;
        for(int i=0; i<shang.length; i++){
            if(shang[i] == answer[i]){
                shangFen +=2;
            }
        }
        System.out.println("小尚的分数:" + shangFen);
        
        int guiFen = 0;
        for(int i=0; i<gui.length; i++){
            if(gui[i] == answer[i]){
                guiFen +=2;
            }
        }
        System.out.println("小硅的分数:" + guiFen);
        
        int guFen = 0;
        for(int i=0; i<gu.length; i++){
            if(gu[i] == answer[i]){
                guFen +=2;
            }
        }
        System.out.println("小谷的分数:" + guFen);
        
        int haoFen = 0;
        for(int i=0; i<hao.length; i++){
            if(hao[i] == answer[i]){
                haoFen +=2;
            }
        }
        System.out.println("小好的分数:" + haoFen);
    }
}

第九题:

案例:从键盘输入本组学员的人数,和本组学员的成绩,用数组存储成绩,然后实现从高到低排序

public class Test01 {
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        
        //(1)先确定人数,即确定数组的长度
        System.out.print("请输入本组学员的人数:");
        int count = input.nextInt();
        
        //(2)创建数组
        int[] scores = new int[count];
        
        //(3)输入学员的成绩
        for(int i=0; i<scores.length; i++){
            System.out.print("请输入第" +(i+1)+"个学员的成绩:");
            scores[i] = input.nextInt();
        }
        
        //(4)排序,冒泡
        for(int i=1; i<scores.length; i++){
            //从高到低,即从大到小
            //例如:5个人
            //从左到右,j的起点是0,
            //终点是3,2,1,0
            //arr[j] 与arr[j+1]
            for(int j=0; j<scores.length-i; j++){
                if(scores[j] < scores[j+1]){
                    int temp = scores[j];
                    scores[j] = scores[j+1];
                    scores[j+1] = temp;
                }
            }
        }
        
        //(5)显示结果
        for(int i=0; i<scores.length; i++){
            System.out.print(scores[i]+ " ");
        }
    }
}

第十题:

案例:从键盘输入本组学员的人数,和本组学员的姓名,用数组存储姓名,然后再从键盘输入一个姓名,查找它是否在之前的数组中,如果存在,就显示它的下标

public class Test02 {
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        
        //(1)先确定人数,即确定数组的长度
        System.out.print("请输入本组学员的人数:");
        int count = input.nextInt();
        
        //(2)创建数组
        String[] names = new String[count];
        
        //(3)输入学员的成绩
        for(int i=0; i<names.length; i++){
            System.out.print("请输入第" +(i+1)+"个学员的姓名:");
            names[i] = input.next();
        }
        
        //(4)输入要查找的学生姓名
        System.out.print("请输入要查找的学生姓名:");
        String name = input.next();
        
        //(5)查找
        int index = -1;
        for(int i=0; i<names.length; i++){
            if(name.equals(names[i])){
                index = i;
                break;
            }
        }
        if(index==-1){
            System.out.println(name+"不存在");
        }else{
            System.out.println(name +"在数组的下标是:" + index);
        }
    }
}

第十一题:

案例:从键盘输入一个英语单词,然后查找这个单词中是否存在'a'字母

提示:把字符串转成字符数组,可以使用String类型的toCharArray()方法

String str = "hello";
char[] arr = str.toCharArray();//arr数组中就是{'h','e','l','l','o'}
public class Test03 {
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);
        
        System.out.print("请输入一个英语单词:");
        String word = input.next();
        
        char[] arr = word.toCharArray();
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == 'a'){
                index = i;
                break;
            }
        }
        if(index==-1){
            System.out.println(word+"中没有a");
        }else{
            System.out.println("a在"+word+"中的下标是:" + index);
        }
    }
}

第十二题:

随机验证码。

  • 随机生成十组六位字符组成的验证码。

  • 验证码由大小写字母、数字字符组成。

开发提示:

  • 使用字符数组保存原始字符,利用Random类生成随机索引。

public class Test04 {
    public static void main(String[] args) {
        String[] arr = new String[10];
        java.util.Random rand = new java.util.Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = "";
            for (int j = 0; j < 6; j++) {
                int num;
                while(true){
                    num = rand.nextInt(123);
                    //数字[48,57]  大写字母[65,90]  小写字母[97,122]
                    if(num>=48 && num<=57){
                        break;
                    }else if(num>=65 && num<=90){
                        break;
                    }else if(num>=97 && num<=122){
                        break;
                    }
                }
                arr[i] += (char)num;
            }
        }
        
        for (int i = 0; i < arr.length; i++) {
            System.out.println("随机验证码:" + arr[i]);
        }
    }
}

二维数组基础题目

第一题:

  • 使用二维数组打印一个 10 行杨辉三角.

    1

    1 1

    1 2 1

    1 3 3 1

    1 4 6 4 1

    1 5 10 10 5 1

    ....

  • 开发提示

  1. 第一行有 1 个元素, 第 n 行有 n 个元素
  2. 每一行的第一个元素和最后一个元素都是 1
  3. 从第三行开始, 对于非第一个元素和最后一个元素的元素.
yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j]


 

public class Test05 {

    public static void main(String[] args){
        //(1)先确定行数
        int[][] yanghui = new int[10][];
        
        //(2)再确定每一行的列数
        //第一行有 1 个元素, 第 n 行有 n 个元素
        for(int i=0; i<yanghui.length; i++){
            yanghui[i] = new int[i+1];
        }
        
        //(3)再确定元素
        for(int i=0; i<yanghui.length; i++){
            //每一行的第一个和最后一个元素都是1
            yanghui[i][0] = 1;
            yanghui[i][i] = 1;
            
            //中间的元素
            for(int j=1; j<yanghui[i].length-1; j++){
                yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
            }
            
        }
        
        //(4)打印显示
        for(int i=0; i<yanghui.length; i++){
            for(int j=0; j<yanghui[i].length; j++){
                System.out.print(yanghui[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

第二题:

  • 打印扑克牌,效果如图所示:

  • 开发提示:

    • 使用一个两行的二维的字符串数组,分别保存花色和点数

public class Test06 {
    public static void main(String[] args){
        //1、声明一个二维数组,并确定长度
        String[][] arr = new String[2][];
        
        //2、确定第一行的列数,第一行存储花色
        arr[0] = new String[4];
        
        //3、确定第二行的列数,第二行存储点数
        arr[1] = new String[13];
        
        //4、把花色和点数放进去
        //花色
        arr[0][0] = "黑桃";
        arr[0][1] = "红桃";
        arr[0][2] = "梅花";
        arr[0][3] = "方片";
        
        //点数
        arr[1][0] = "A";
        for(int i=1; i<=9; i++){//表示第二行部分下标
            arr[1][i] = i+1+"";
        }
        arr[1][10] = "J";
        arr[1][11] = "Q";
        arr[1][12] = "K";
    
        //5、显示
        for(int i=0; i<arr[0].length; i++){//外循环循环花色
            for(int j=0; j<arr[1].length; j++){//内循环循环点数
                //arr[0][?]是花色
                //arr[1][?]是点数
                System.out.print(arr[0][i] + arr[1][j] + " ");
            }
            System.out.println();
        }
    }
}

第三题:

需求:保存全班的每个组的成绩,并对成绩做统计

​​​​​​
  1. 从键盘输入一共有几组

  2. 从键盘输入每一组分别有多少人

  3. 从键盘输入每一个同学的成绩

  4. 统计每一组的最高分、最低分

  5. 统计每一组的平均分

  6. 统计全班的最高分、最低分

  7. 统计全班的平均分

  8. 统计全班的总人数

public class Test07 {
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        
        //(1)从键盘输入一共有几组,确定二维数组的行数
        System.out.print("请输入一共有几组:");
        int group = input.nextInt();
        
        //(2)创建二维数组,并确定行数
        int[][] arr = new int[group][];
        
        //(3)从键盘输入每一组分别有多少人,确定每一行的列数
        for(int i=0; i<arr.length; i++){
            System.out.print("请输入第" + (i+1) + "组有几个人:");
            arr[i] = new int[input.nextInt()];
            
            //(4)从键盘输入每一个同学的成绩
            for(int j = 0; j<arr[i].length; j++){
                System.out.print("请输入第" + (j+1) + "个组员的成绩:");
                arr[i][j] = input.nextInt();
            }
        }
        
        //(4)显示成绩表
        System.out.println("每组成绩如下:");
        for(int i=0; i<arr.length; i++){
            for(int j=0; j<arr[i].length; j++){
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
        
        /*
        4. 统计每一组的最高分、最低分
        5. 统计每一组的平均分
        6. 统计全班的最高分、最低分
        7. 统计全班的平均分
        8. 统计全班的总人数
        */
        int[] groupMax = new int[group];//有几组就有几个元素
        int[] groupMin = new int[group];
        double[] groupAvg = new double[group];
        int max = -1;//全班的最高分
        int min = 101;//全班的最低分,假设成绩的范围是[0,100]
        double sum = 0;
        int count = 0 ;
        
        //把每一组的最高分,最低分,都初始化为特殊值
        for(int i=0; i<group; i++){
            groupMax[i] = -1;
            groupMin[i] = 101;
        }
        
        //统计
        for(int i=0; i<arr.length; i++){
            double groupSum = 0;//每一组累加总分,都是从0开始
            for(int j=0; j<arr[i].length; j++){
                if(arr[i][j] > groupMax[i]){//找每组的最高分
                    groupMax[i] = arr[i][j];
                }
                if(arr[i][j] < groupMin[i]){//找每组的最低分
                    groupMin[i] = arr[i][j];
                }
                if(arr[i][j] > max){//找全班的最高分
                    max = arr[i][j];
                }
                if(arr[i][j] < min){//找全班的最低分
                    min = arr[i][j];
                }
                groupSum += arr[i][j];//累加每一组的总分
                sum += arr[i][j];//累加全部的总分
                count++;//累加总人数
            }
            
            //每一组的平均分 = 每一组的总分/每一组的人数
            groupAvg[i] = groupSum / arr[i].length;
        }
        
        //全部的平均分 = 全部的总分 / 总人数
        double avg = sum / count;
        
        System.out.println("全班的最高分:" + max);
        System.out.println("全班的最低分:" + min);
        System.out.println("全班的平均分:" + avg);
        System.out.println("全班的总人数:" + count);
        for(int i=0; i<arr.length; i++){
            System.out.println("第" + (i+1) + "组的最高分:" + groupMax[i]);
            System.out.println("第" + (i+1) + "组的最低分:" + groupMin[i]);
            System.out.println("第" + (i+1) + "组的平均分:" + groupAvg[i]);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骨灰级宅神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值