第二次上机练习
1 ..编写程序, 输入变量x的值,如果是 1 ,输出x= 1 ,如果是 5 ,输出x= 5 ,如果是 10 ,输出 x= 10 ,除了以上几个值,都输出x=none。(知识点: if 条件语句) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package test3; import java.util.Scanner; public class shangji2 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "请输入x的值" ); Scanner input= new Scanner(System.in); int x=input.nextInt(); if (x == 1 || x == 5 || x == 10 ) { System.out.println( "x=" + x); } else { System.out.println( "x=none" ); } } } |
2.用switch结构实现第1题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package test3; import java.util.Scanner; public class shangji2 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "请输入x的值" ); Scanner input= new Scanner(System.in); int x=input.nextInt(); switch (x){ case 1 : case 5 : case 10 :System.out.println( "x=" +x); break ; default :System.out.println( "x=none" ); } } } |
3.判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整 除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package test3; import java.util.Scanner; public class shangji2 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "请输入一个数字" ); Scanner input= new Scanner(System.in); int x=input.nextInt(); if (x% 5 == 0 &&x% 6 == 0 ) { System.out.println( "能被5和6整除" ); } else if (x% 5 == 0 ){ System.out.println( "能被5整除" ); } else if (x% 6 == 0 ){ System.out.println( "能被6整除" ); } else if (x% 5 != 0 ||x% 6 != 0 ){ System.out.println( "不能被5和6整除" ); } } } |
4.输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印 A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package test3; import java.util.Scanner; public class shangji2 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "请输入一个0-100的数字" ); Scanner input= new Scanner(System.in); int x=input.nextInt(); if (x> 100 ){ System.out.println( "分数无效" ); } else if (x>= 90 ) { System.out.println( "A" ); } else if (x>= 80 &&x<= 89 ){ System.out.println( "B" ); } else if (x>= 70 &&x<= 79 ){ System.out.println( "C" ); } else if (x>= 60 &&x<= 69 ){ System.out.println( "D" ); } else if (x<= 59 ){ System.out.println( "E" ); } } } |
5.输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package test3; import java.util.Scanner; public class shangji2 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input= new Scanner(System.in); System.out.println( "请输入x的值" ); int x=input.nextInt(); System.out.println( "请输入y的值" ); int y=input.nextInt(); System.out.println( "请输入z的值" ); int z=input.nextInt(); if (x>y && y>z) { System.out.println( "输出" +z+ "," +y+ "," +x); } else if (x>z && z>y) { System.out.println( "输出" +y+ "," +z+ "," +x); } else if (y>x && x>z) { System.out.println( "输出" +z+ "," +x+ "," +y); } else if (y>z && z>x) { System.out.println( "输出" +x+ "," +z+ "," +y); } else if (z>y && y>x) { System.out.println( "输出" +x+ "," +y+ "," +z); } else if (z>x && x>y) { System.out.println( "输出" +y+ "," +x+ "," +z); } |