//给一个成绩
int i = 70;
//设置给出成绩的范围
if (i <= 100 && i >= 0) {
//缩小范围并判断然后匹配
switch (i/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;
default : System.out.println("E等");
break;
//提示错误的输入
}else {
System.out.println("您的输入有误,请重新输入");
}
}
第二种:
import java.util.Scanner;
class Test4_Switch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int j = (i <= 100 && i >= 0 )? i / 10 : 102;
switch (j) {
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 0:
case 1:
case 2:
case 3:
case 4:
case 5:System.out.println("E等");
break;
default : System.out.println("您的成绩输入错误");
}
}
} |