2014-02-22 76 views
0

有兩個問題。我聽說最好使用switch語句,當你有超過3個'if'語句時。是這樣嗎?Java:對多表達式使用switch語句?

其次,可能有人查看下面的代碼,並給我一個想法就A.如何轉換成switch語句雖然是多表達或B.解釋爲什麼變量「級」總是導致F.

while (!"Y".equalsIgnoreCase(exit)) 
     { 
       i++; 
       System.out.print("Please enter name of assignment " + i + ": "); 
       assignment = user.nextLine().toUpperCase(); //consumes string + \n 
       System.out.print("Please enter points earned: "); 
       earned = user.nextDouble();//consumes double 
       user.nextLine();//consumes \n 
       System.out.print("Please enter total points possible: "); 
       total = user.nextDouble(); 
       user.nextLine(); 



     System.out.print("Assignment\t"); System.out.print("Score\t"); System.out.print("Total Points\t"); System.out.print("Percentage\t\n"); 


     System.out.print(assignment + "\t\t"); System.out.print(earned + "\t"); System.out.print(total + "\t\t"); System.out.print(percent.format(earned/total) + "\n\t"); 


     // Putting user data into an array for calculation: 
      double[] calc; calc = new double[4]; 
       calc[i] = earned; // Array elements coincide with the current iteration loop 

      double[] totalPoints; totalPoints = new double[4]; 
       totalPoints[i] = total; 


     for (double e : calc) // adds up all the stored data in the array 
      sum += e; 


     for (double f : totalPoints) // adds up all the stored data in the array 
      tot += f; 




    char grade = '0'; 
      if (sum/tot >= 90) 
      { 
       grade = 'A'; 
      } 

      else if (sum/tot >= 80 && sum/tot <= 89.99) 
      { 
       grade = 'B'; 
      } 

      else if (sum/tot >= 70 && sum/tot <= 79.99) 
      { 
       grade = 'C'; 

      } 

      else if (sum/tot >= 60 && sum/tot <= 69.99) 
      { 
       grade = 'D'; 

      } 

      else if (sum/tot <= 59.99) 
      { 
       grade = 'F'; 

      } 
     System.out.println("Your total is " + sum + " out of " + tot + ", or " + percent.format(sum/tot) + ", and your grade is an " + grade); 

     } 
+1

如果變量持有這聽起來像他們持有,你需要'100'來劃分所有閾值。 – Keppil

+0

如果不知道'sum'和'tot'的值,任何人都可以發表評論。 –

+0

'switch'正在處理具體的值,而不是範圍 – nikis

回答

0

正如@Keppil提到,sum/tot可能總是產生0和1之間值。您可能想要更改您要比較的閾值(即:0.9而不是90等等)。

在另一方面,當你還沒有滿足如果(和/ TOT> = 90)條件,你肯定知道sum/tot嚴格小於90。因此,如果sum/tot <= 89.99不需要檢查以下測試。順便說一句,這將是危險的:想象一下,如果sum/tot == 89.999?你的任何條件都不會得到滿足。

我建議創建產生檔次,這樣的方法:

public static char gradeFor(final float tot, final float sum) { 
    float ratio = tot/sum; 

    if(ratio >= 0.9f) { 
     return 'A'; 
    } 

    if(ratio >= 0.8f) { 
     return 'B'; 
    } 

    if(ratio >= 0.7f) { 
     return 'C'; 
    } 

    if(ratio >= 0.6f) { 
     return 'D'; 
    } 

    if(ratio >= 0.5f) { 
     return 'E'; 
    } 

    return 'F'; 
} 

最後一兩件事:它似乎沒有在其中您可以返回è情況下...是意?

總之,我相信,你可能想看看這個:

  • 確保totsum是十進制數
  • 確保您的閾值符合你對測試值(你最有可能必須將它們除以100f,正如@Keppil所提到的)
  • 確保沒有錯過的邊角情況(例如,如果tot/sum大於89。99低於90
  • 確保你實施的行爲是什麼,你打算做(事實上,你永遠不能返回'E'有點困擾我:P)

乾杯!

0

我不認爲一個開關的情況下會在這種情況下工作得很好,但像keppil上面說的,你應該評估的if語句與不同的閾值的浮動,即

private float total = sum/tot * 100;

if(total >= 90){ //etc...

因爲它現在站在那筆/ TOT計算結果爲小數

0

當存在一系列需要被檢查,那麼,如果語句是合適的值的病打賭。
當你有特定的值來檢查時,switch語句是合適的。

在你的代碼中,如果語句正好。雖然你可以通過編寫類似以下改進:

public class Grade 
{ 
    public Grade() 
    { 
    float score = 95; 
    float total = 100; 

    char grade = getGrade(score); 

    System.out.println("Score: " + score); 
    System.out.println("Total: " + total); 
    System.out.println("Grade: " + grade); 
    } 

    private char getGrade(float score) 
    { 
    char grade = 'F'; 

    if (score >= 90) 
     grade = 'A'; 

    else if (score >= 80) 
     grade = 'B'; 

    else if (score >= 70) 
     grade = 'C'; 

    else if (score >= 60) 
     grade = 'D'; 

    return grade; 
    } 

    public static void main(String[] args) 
    { 
    new Grade(); 
    } 
} 
0

switch語句是不是一件好事,use.Why?嗯,switch語句只能用原始數據類型和枚舉類型的工作。但是,您可以使用包裝類基本類型,如字節,雙打,INT等