java练习:采用switch,不能使用if
/*
假设系统给定考生成绩,判断该考生的成绩等级
1.有效范围:0-100
2.考试成绩可能带有小数
3.考试成绩和等级对照表
90-100 A
80-90 B
70-80 C
60-70 D
0-60 E
4.以上需求必须采用switch语句完成,不能使用if
思路: (int)(成绩/10)
90/10==9
80/10==8
82.5/10==8
....
总结:用成绩来除以10,强制转换int类型,所得结果在:0,1,2,3,4,5,6,7,8,9,10。
*/
public class Student
{
public static void main(String[] args){
double fraction = 85;
int grade = (int)(fraction/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");
}
}
}