1.编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出
x=10,除了以上几个值,都输出x=none。(知识点:if条件语句)
package work; import java.util.Scanner; public class text { public static void main(String args[]){ Scanner in=new Scanner(System.in); int x=in.nextInt(); if(x==1) { x=1; System.out.println("x="+x); } else if(x==5) { x=5; System.out.println("x="+x); } else if(x==10) { x=10; System.out.println("x="+x); } else { System.out.println("x=none5"); } } }
2.用switch结构实现第1题
package work; import java.util.Scanner; public class text { public static void main(String args[]){ Scanner in=new Scanner(System.in); int x=in.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整除)
package work; import java.util.Scanner; public class text { public static void main(String args[]){ Scanner in=new Scanner(System.in); System.out.println("输入x:"); int x=in.nextInt(); if(x%30==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 { System.out.println("不能被5和6整除"); } } }
4.输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印
A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)
package work; import java.util.Scanner; public class text { public static void main(String args[]){ Scanner in=new Scanner(System.in); System.out.println("输入x:"); int grade=in.nextInt(); if((grade>100)||(grade<0)) { System.out.println("超出范围"); } else if((grade>=0)&&(grade<=59)) { System.out.println("不及格"); } else if((grade>=60)&&(grade<=79)) { System.out.println("及格"); } else { System.out.println("优秀"); } } }
5.输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)
package work; import java.util.Arrays; import java.util.Scanner; public class text { public static void main(String args[]){ System.out.println("请输入三个整数:"); Scanner input=new Scanner(System.in); int []num =new int[3]; num[0]=input.nextInt(); num[1]=input.nextInt(); num[2]=input.nextInt(); Arrays.sort(num); for (int i = 0; i < num.length; i++) { System.out.println(num[i]); } } }