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