2013-11-25 107 views
2

我試圖使用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; 
    } 
} 

} 
+4

所以,如果'points'是等於2,你檢查'如果(點> = 60 &&分數<= 89)'。你認爲這是邏輯嗎? –

+0

該switch語句有什麼意義? – Artur

+0

還需要休息;對於每種情況,請參閱下面的答案 –

回答

2

雖然上int類型變量切換說points,對應於殼體n的代碼被執行,當points值爲n。我想你現在可以明白錯誤在哪裏了。目前,您的任何案例都不符合points的值,如果它大於6,則值將爲null

很明顯,您希望執行的案例可以在各種範圍內執行。實現這一點的一種方法是將範圍縮小到較小範圍,以便爲他們編寫案例更容易。例如,通過將points除以10,範圍[0, 59]可以縮小爲[0, 5]。同樣,對於其它範圍,可以拉近他們,並寫出情況下,像這些:

public static Grade findGrade(int points) { 
    int val = points/10; 

    switch (val) { 
     // 0 <= points <= 59 is same as 0 <= val <= 5 
     case 0: case 1: 
     case 2: case 3: 
     case 4: case 5: return Grade.valueOf("U"); 

     // 60 <= points <= 89 is same as 6 <= val <= 8 
     case 6: case 7: 
     case 8:   return Grade.valueOf("E"); 

     // like so 
    } 
} 
+0

你的意思是'points/10'。 – Zong

+0

@宗正立哎呀。修正了謝謝:) –

+0

一個糟糕的主意恕我直言 - 正確的答案只是不使用'開關'。 – jwg

5

讓我們看看

switch (points) { 
    case 1: 
     if (points >= 0 && points <= 59) { 
      return Grade.valueOf("U"); 
     } 

什麼你基本上說的是:

if (points == 1) { 
    if (points >= 0 && points <= 59) { 
     return Grade.valueOf("U"); 
    } 
} 

這是廢話。在這種情況下,我認爲你根本不需要開關。只需使用:

if (points < 0) { 
    return null; 
} 
if (points <= 59) { 
    return Grade.valueOf("U"); 
} 
if (points <= 89) { 
    return Grade.valueOf("E"); 
} 
if (points <= 119) 
    return Grade.valueOf("D"); 
} 
... 
return null; 
2

根本不需要開關。簡而言之鏈中的你if S,這將爲你工作:

if (points>= 0 && points <= 59) { 
    return Grade.valueOf("U"); 
} 
if (points >= 60 && points <= 89) { 
    return Grade.valueOf("E"); 
} 
if (points >= 90 && points <= 119) { 
    return Grade.valueOf("D"); 
} 
if (points >= 110 && points <= 139) { 
    return Grade.valueOf("C"); 
} 
if (points >= 140 && points <= 169) { 
    return Grade.valueOf("B"); 
} 
if (points >= 170 && points <= 200) { 
    return Grade.valueOf("A"); 
} 
return null; 

switch說法背後的想法是讓你立足於一組固定值或小值範圍的決定。但是,在這種情況下,範圍非常大,因此連鎖的if語句看起來更合適。

考慮到要實現的任務,你可以建立一個查找表更短的解決方案,一個循環:

int[] limits = new int[] {59, 89, 119, 139, 169, 200}; 
String[] grades = new String[] {"U", "E", "D", "C", "B", "A"}; 
for (int i = 0 ; i != limits.length ; i++) 
    if (points <= limits[i]) 
     return grades[i]; 
return null;