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