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