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