1.编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。(知识点:if条件语句)
package xz1; public class wwx { public static void main(String[] args) { // TODO Auto-generated method stub int x=0; if(x==1){ System.out.println("x=1"); }else if(x==5){ System.out.println("x=5"); }else if(x==10){ System.out.println("x=10"); }else{ System.out.println("x=none"); } } }
2.用switch结构实现第1题
package xz1; public class wwx { public static void main(String[] args) { // TODO Auto-generated method stub int x=1; switch(x){ case 1: System.out.println("x=1");break; case 5: System.out.println("x=5");break; case 10: System.out.println("x=10");break; default: System.out.println("x=none"); } } }
3.判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整 除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
package xz1; import java.util.Scanner; public class wwx { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入一个整数:"); Scanner scanner=new Scanner(System.in); int x=scanner.nextInt(); if(x%5==0&&x%6==0){ System.out.println("输入的数字"+x+"能被5和6同时整除"); }else if(x%5==0){ System.out.println("输入的数字"+x+"能被5整除"); }else if(x%6==0){ System.out.println("输入的数字"+x+"能被6整除"); }else{ System.out.println("输入的数字不能被5或6整除"); } } }
4.输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印 A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)
package xz1; import java.util.Scanner; public class wwx { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入分数:"); Scanner x=new Scanner(System.in); int num=x.nextInt(); if(num<0||num>100){ System.out.println("打印分数无效"); }else if(num>0||num<60){ System.out.println("E"); }else if(num>60||num<70){ System.out.println("D"); }else if(num>70||num<80){ System.out.println("C"); }else if(num>80||num<90){ System.out.println("B"); }else if(num>90||num<100){ System.out.println("A"); } } }
5.输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)
package xz1; import java.util.Scanner; public class wwx { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入三个整数:"); Scanner i=new Scanner(System.in); int x=i.nextInt(); int y=i.nextInt(); int z=i.nextInt(); if(x<y){ int t=x;x=y;y=t; }if(x<z){ int t=x;x=z;z=t; }if(y<z){ int t=y;y=z;z=t; }System.out.println(z+""+y+""+x); } }