第二次上机作业
- 编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。(知识点:if条件语句)
package day; import java.util.Scanner; public class hh { public static void main(String[] args){ System.out.println("请输入x的值:"); Scanner input=new Scanner(System.in); int x=input.nextInt(); if(x==1||x==5||x==10){ System.out.println("x="+x); } else{ System.out.println("x=none"); } } }
-
package day; import java.util.Scanner; public class hh { public static void main(String[] args){ System.out.println("请输入x的值:"); Scanner input=new Scanner(System.in); int x=input.nextInt(); 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"); } } }
-
package day; import java.util.Scanner; public class hh { public static void main(String[] args){ System.out.println("请输入一个数:"); Scanner input=new Scanner(System.in); int x=input.nextInt(); if(x%5==0&&x%6==0){ System.out.println("能被5和6整除"); }else if(x%6==0&&x%5!=0){ System.out.println("只能被6整除"); }else if(x%5==0&&x%6!=0){ System.out.println("只能被5整除"); }else{ System.out.println("不能被5或6整除"); } } }
-
输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印 A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)
package day; import java.util.Scanner; public class hh { public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println(" 输入分数:"); Scanner input = new Scanner(System.in); int x=input.nextInt(); if(x<0||x>100){ System.out.println("无效"); }else if(x>=90){ System.out.println("A"); }else if(x>=80){ System.out.println("B"); }else if(x>=70){ System.out.println("C"); }else if(x>=60){ System.out.println("D"); }else if(x<60){ System.out.println("E"); } } }
- 输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)
package day; import java.util.Scanner; public class hh { public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("请输入三个整数:"); int x=sc.nextInt(); int y=sc.nextInt(); int z=sc.nextInt(); int d; if(x>y){ d=y; y=x; x=d; } if(x>z){ d=z; z=x; x=y; } if(y>z){ d=z; z=y; y=d; } System.out.println(x+" "+y+" "+z); } }