第四周作业(上机)
1.编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。(知识点:if条件语句)
1 import java.util.*; 2 public class wyyy { 3 4 /** 5 * @param args 6 */ 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 System.out.println("请输入x的值"); 10 Scanner input=new 11 Scanner(System.in); 12 int x=input.nextInt(); 13 if(x==1){ 14 System.out.println("x="+x); 15 }else if (x==5){ 16 System.out.println("x="+x); 17 }else if(x==10){ 18 System.out.println("x="+x); 19 }else{ 20 System.out.println("x=none"); 21 } 22 } 23 }
2.用switch结构实现第题1
1 import java.util.*; 2 public class wyy { 3 public static void main(String[] args) 4 { 5 System.out.println("请输入x的值"); 6 Scanner input=new Scanner(System.in); 7 int x = input.nextInt(); 8 switch(x/1) 9 { 10 case 1: System.out.println("x="+x);break; 11 case 5: System.out.println("x="+x);break; 12 case 10: System.out.println("x="+x);break; 13 default: System.out.println("x=none");break; 14 } 15 16 } 17 18 }
3.判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整 除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
1 import java.util.*; 2 public class wyy { 3 public static void main(String[] args) 4 { 5 System.out.println("请输入一个数字"); 6 Scanner input=new Scanner(System.in); 7 int x = input.nextInt(); 8 if(x%5==0&&x%6==0) 9 { 10 System.out.println("能同时被5和6整除"); 11 } 12 else if(x%5==0) { 13 System.out.println("能被5整除"); 14 } 15 else if(x%6==0) { 16 System.out.println("能被6整除"); 17 } 18 else { 19 System.out.println("不能被5和6整除"); 20 } 21 } 22 23 }
4.输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印 A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)
1 import java.util.*; 2 public class wyy { 3 public static void main(String[] args) { 4 System.out.println("请输入一个0到100的分数"); 5 Scanner input=new Scanner(System.in); 6 double x = input.nextDouble(); 7 if(x>100||x<0) { 8 System.out.println("分数无效"); 9 } 10 else if(x>=90&&x<=100) { 11 System.out.println("A"); 12 } 13 else if(x>=80&&x<=89) { 14 System.out.println("B"); 15 } 16 else if(x>=80&&x<=79) { 17 System.out.println("C"); 18 } 19 else if(x>=70&&x<=69) { 20 System.out.println("D"); 21 } 22 else { 23 System.out.println("E"); 24 } 25 26 } 27 28 }
5.输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)
1 import java.util.*; 2 public class wyy{ 3 public static void main(String[] args) { 4 System.out.println("请输入三个整数,由小到大排列"); 5 Scanner input=new Scanner(System.in); 6 int x=input.nextInt(); 7 int y=input.nextInt(); 8 int z=input.nextInt(); 9 int one,two,three; 10 one=x; 11 if(y<x&&y<z){ 12 one=y; 13 } 14 if(z<x&&z<y){ 15 one=z; 16 } 17 three=x; 18 if(y>x&&y>z){ 19 three=y; 20 } 21 if(z>x&&z>y){ 22 three=z; 23 } 24 two=(x+y+z)-(one+three); 25 System.out.println(one+","+two+","+three); 26 } 27 28 }