我試圖使用enum和switch語句爲測試實現分級系統,但是使用當前代碼我得到的結果始終爲「null」。我看不出哪裏出了問題,誰能幫忙?Switch語句返回null
public enum Grade {
A, B, C, D, E, U;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the students mark:");
int points = scan.nextInt();
if (points < 0 || points > 200) {
System.out.println("Error! points must be between 0 & 200");
} else {
System.out.println(findGrade(points));
}
}
public static Grade findGrade(int points) {
switch (points) {
case 1:
if (points>= 0 && points <= 59) {
return Grade.valueOf("U");
}
case 2:
if (points >= 60 && points <= 89) {
return Grade.valueOf("E");
}
case 3:
if (points >= 90 && points <= 119) {
return Grade.valueOf("D");
}
case 4:
if (points >= 110 && points <= 139) {
return Grade.valueOf("C");
}
case 5:
if (points >= 140 && points <= 169) {
return Grade.valueOf("B");
}
case 6:
if (points >= 170 && points <= 200) {
return Grade.valueOf("A");
}
default:
return null;
}
}
}
所以,如果'points'是等於2,你檢查'如果(點> = 60 &&分數<= 89)'。你認爲這是邏輯嗎? –
該switch語句有什麼意義? – Artur
還需要休息;對於每種情況,請參閱下面的答案 –