1.编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。(知识点:if条件语句)
package apple; public class Play { public static void show(int x) { if (x == 1 || x == 5 || x == 10) { System.out.println("x=" + x); } else { System.out.println("x=none"); } } public static void main(String[] args) { show(1); show(2); show(5); } }
2.用switch结构实现第1题
package apple; public class Play { public static void xValue(int x) { 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"); break; } } public static void main(String[] args) { xValue(45);//6 } }
3.判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整 除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
package apple; public class Play { public static void exactDivisionFiveAndSix(int num) { if((num%5 == 0) && (num%6 == 0)) { System.out.println(num + " 能被5和6整除"); }else if((num%5 == 0) && (num%6 != 0)){ System.out.println(num + " 能被5整除"); }else if((num%5 != 0) && (num%6 == 0)) { System.out.println(num + " 能被6整除"); }else { System.out.println(num + " 不能被5或6整除"); } } public static void main(String[] args) { exactDivisionFiveAndSix(3);//7 } }
4.输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印 A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)
package apple; public class Play { public static void scoreGrade2(int score) { if(score > 100 || score < 0) { System.out.println("给定成绩范围应该在0到100之间 " + score + "超出边界"); }else { int grade = score / 10; switch (grade) { case 10: case 9: System.out.println("您的成绩等绩等级为 A "); break; case 8: System.out.println("您的成绩等绩等级为 B "); break; case 7: System.out.println("您的成绩等绩等级为 C "); break; case 6: System.out.println("您的成绩等绩等级为 D "); break; default: System.out.println("您的成绩等绩等级为 E "); break; } } } public static void main(String[] args) { scoreGrade2(90);//9 } }
5.输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)
package apple; public class Play { public static void sortAscNum(int num_x,int num_y,int num_z) { if(num_x > num_y && num_y > num_z) { System.out.print(num_z); System.out.print(" " + num_y); System.out.print(" " + num_x); }else if (num_x < num_y && num_y < num_z) { System.out.print(num_x); System.out.print(" " + num_y); System.out.print(" " + num_z); }else if (num_x > num_y && num_y < num_z && num_x > num_z) { System.out.print(num_y); System.out.print(" " + num_z); System.out.print(" " + num_x); }else if(num_x > num_y && num_y < num_z && num_x < num_z){ System.out.print(num_y); System.out.print(" " + num_x); System.out.print(" " + num_z); }else if(num_x < num_z && num_x < num_y && num_z > num_y){ System.out.print(num_x); System.out.print(" " + num_z); System.out.print(" " + num_y); }else { System.out.print(num_z); System.out.print(" " + num_x); System.out.print(" " + num_y); } } public static void main(String[] args) { sortAscNum(23,12,7);//10 } }