2020.3.26上机作业

第一题:编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 
x=10,除了以上几个值,都输出x=none。(知识点:if条件语句)

 1 package test1;
 2 import java.util.Scanner;
 3 public class one {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("请输入x的值:");
 8         Scanner input=new Scanner(System.in);
 9         int x=input.nextInt();
10         if(x==1) {
11             System.out.println("x="+1);
12         }else if(x==5) {
13             System.out.println("x="+5);
14             
15         }else if(x==10) {
16             System.out.println("x="+10);
17         }else {
18             System.out.println("none");
19         }
20         
21 
22     }
23 
24 }

第二题:用switch结构实现第1题

 1 package test1;
 2 import java.util.Scanner;
 3 public class one {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("请输入x的值:");
 8         Scanner input=new Scanner(System.in);
 9         int x=input.nextInt();
10          switch(x) {
11          case 1:
12          case 5:
13          case 10:
14              System.out.println("x="+x);
15              break;
16          default:
17              System.out.println("x=none");
18          }
19     }
20 }

第三题:判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整 
除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)

 1 package test1;
 2 import java.util.Scanner;
 3 public class one {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("请输入x的值:");
 8         Scanner input=new Scanner(System.in);
 9         int x=input.nextInt();
10         if(x%5==0&&x%6==0) {
11             System.out.println("能被5和6整除");
12         }else if(x%5==0&&x%6!=0) {
13             System.out.println("只能被5整除");    
14         }else if(x%6==0&&x%5!=0) {
15             System.out.println("只能被6整除");
16         }else if(x%5!=0||x%6!=0) {
17             System.out.println("不能被5或6整除");
18         }else {
19         System.out.println("重新输入");
20         } 
21 
22     }
23 }

第四题:输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印 
A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)

 1 package test1;
 2 import java.util.Scanner;
 3 public class one {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("请输入分数:");
 8         Scanner input=new Scanner(System.in);
 9         int x=input.nextInt();
10          if(x<=100&&x>=90) {
11             System.out.println("等级为A!");
12         }else if(x<=89&&x>=80) {
13             System.out.println("等级为B!");
14         }else if(x<=79&&x>=70) {
15             System.out.println("等级为C!");
16         }else if(x<=69&&x>=60) {
17             System.out.println("等级为D!");
18         }else if(x<=59&&x>=0){
19             System.out.println("等级为E!");
20         }else {
21             System.out.println("重新输入!");
22         }
23     }
24 }

第五题:输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)

 1 package test1;
 2 import java.util.Scanner;
 3 public class one {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("请分别输入三个值:");
 8         Scanner input=new Scanner(System.in);
 9         int n;
10         int x=input.nextInt();
11         int y=input.nextInt();
12         int z=input.nextInt();
13         if(x>y&&y>z){
14             System.out.println(" "+z+" "+y+" "+x);
15         }else if(y>x&&x>z){
16             System.out.println(" "+y+" "+z+" "+x);
17         }else if(z>x&&x>y){
18             System.out.println(" "+y+" "+x+" "+z);
19         }else if(y>z&&z>x){
20             System.out.println(" "+x+" "+z+" "+y);
21         }else if(x>z&&z>y){
22             System.out.println(" "+y+" "+z+" "+x);
23         }else if(z>y&&y>x){
24             System.out.println(" "+x+" "+y+" "+z);
25         }
26     }
27 }

猜你喜欢

转载自www.cnblogs.com/919753740yu/p/12573493.html