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