需求:键盘录入一个成绩(整数类型),判断并输出成绩的等级。
要求使用两种方式:if和switch
成绩及对应等级如下
90~100 A
80~89 B
70~79 C
60~69 D
0~59 E
If:
packagecom.briup.day5;
importjava.util.Scanner;
public class Test3{
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = sc.nextInt();
if(90 <= score && score<= 100) {
System.out.println("A");
} else if(80 <= score&& score <= 89) {
System.out.println("B");
} else if(70 <= score&& score <= 79) {
System.out.println("C");
} else if(60 <= score&& score <= 69){
System.out.println("D");
} else if(score <= 59) {
System.out.println("E");
} else {
System.out.println("成绩录入错误")
}
}
}
Switch:
packagecom.briup.day5;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = sc.nextInt();
switch(score/10) {
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;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
System.out.println("E");
default:
System.out.println("成绩录入错误");
break;
}
}
}