2015-11-24 165 views
0

我無法弄清楚我的程序的問題,非常感謝任何幫助!不幸的是,我是一名初學者程序員......當我運行程序時,它會正確地請求課程數量,學分和成績,但它忽略輸入的學分,並給出了字母等級的正常值。當最後它顯示「你的GPA爲0.0」時,顯然這是不正確的。再次感謝!Java GPA計算器問題

public class QualityPoints 
{ 
public static void main(String[] args) 
{ 
    // Needed variables 

    String grade; 
    int totalCredits = 0; 
    int totalCreditsEarned = 0; 
    int credits; 
    int classes; 
    double gpa; 
    double number=0; 

    String greeting = "This program will calculate your GPA."; 
    JOptionPane.showMessageDialog(null, greeting, "GPA Calculator", 1); 


    classes = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of classes you are taking")); 



    //Loop that ends once the student has put information in on all his classes 
    for(int count = 0; count < classes; count++) 
    { 
     credits = Integer.parseInt(JOptionPane.showInputDialog(null, "How many credit was this class?:")); 
     //reads the letter grade using the String Grade prompt 



    // gathers input from user and assigns a string into JOptionPane 
    grade = JOptionPane.showInputDialog(null, "Enter letter grade: ", 
      "Quality Points Converter", JOptionPane.INFORMATION_MESSAGE); 
    // calls separate method (computeQualityPoints) using parameter grade 




     if (!grade.equalsIgnoreCase("a") && !grade.equalsIgnoreCase("a-") 
       && !grade.equalsIgnoreCase("b+") && !grade.equalsIgnoreCase("b") 
       && !grade.equalsIgnoreCase("b-") && !grade.equalsIgnoreCase("c+") 
       && !grade.equalsIgnoreCase("c") && !grade.equalsIgnoreCase("c-") 
       && !grade.equalsIgnoreCase("d+") && !grade.equalsIgnoreCase("d") 
       && !grade.equalsIgnoreCase("d-") && !grade.equalsIgnoreCase("f")) { 
      JOptionPane.showMessageDialog(null, "Invalid grade entered"); 
     } else { 
      JOptionPane.showMessageDialog(null, "You received " 
        + computeQualityPoints(grade) + " quality points"); 
      computeQualityPoints(grade); 

     } 

     //algorithm for finding the GPA 
     totalCredits += credits; 
     totalCreditsEarned += (credits * number); 
     } 
     //for loop ends 

     //GPA is calculated for all the students classes 
     gpa = totalCreditsEarned/totalCredits; 

    JOptionPane.showMessageDialog(null, "Your GPA is: " + gpa); 

} 

/** 
* Uses the letter grade given as the parameter to compute quality points 
* received, thus displaying quality points as the output 
* 
* @param grade 
* @return JOptionPane message box with the number of quality points, given 
*   a valid letter grade. 
*/ 

public static double computeQualityPoints(String grade) { 

    /** 
    * If/else statments providing the message attached to the output 
    * corresponding to the grade 
    */ 


    if (grade.equalsIgnoreCase("a")) { 
     return 4.0; 
    } 
    if (grade.equalsIgnoreCase("a-")) { 
     return 3.7; 
    } 
    if (grade.equalsIgnoreCase("b+")) { 
     return 3.3; 
    } 
    if (grade.equalsIgnoreCase("b")) { 
     return 3.0; 
    } 
    if (grade.equalsIgnoreCase("b-")) { 
     return 2.7; 
    } 
    if (grade.equalsIgnoreCase("c+")) { 
     return 2.3; 
    } 
    if (grade.equalsIgnoreCase("c")) { 
     return 2.0; 
    } 
    if (grade.equalsIgnoreCase("c-")) { 
     return 1.7; 
    } 
    if (grade.equalsIgnoreCase("d+")) { 
     return 1.3; 
    } 
    if (grade.equalsIgnoreCase("d")) { 
     return 1.0; 
    } 
    if (grade.equalsIgnoreCase("d-")) { 
     return 0.7; 
    } 
    if (grade.equalsIgnoreCase("f")) { 
     return 0.0; 
    } 
    return 0.0; 
} 
} 

回答

1
totalCreditsEarned += (credits * number); 

number保持0.0,這意味着totalCreditsEarnedgpa也將保持0.0

我懷疑

computeQualityPoints(grade); 

你在哪裏忽略了返回值,應該是

number = computeQualityPoints(grade); 

(至少我假定這就是number應該包含)

0

由於很明顯,您忘記了computeQualityPoints的結果,並將其存儲在number變量中。您顯示的信息給用戶後,您應該更改從行:

JOptionPane.showMessageDialog(null, "You received " 
        + computeQualityPoints(grade) + " quality points"); 
computeQualityPoints(grade); 

JOptionPane.showMessageDialog(null, "You received " 
        + computeQualityPoints(grade) + " quality points"); 
number = computeQualityPoints(grade); 

但其他一些提示,以提高你的代碼質量:

你可以請求用戶在循環中輸入N' th級信用:

credits = Integer.parseInt(JOptionPane.showInputDialog(null, "How many credit was "+(count+1)+"'th class?")); 

Thi s爲您的用戶提供了更好的體驗。

您也可以更改檢查進入等級這是在你的程序靜態的有效性的方式,通過將它們存儲在一個地圖結構如HashMap

public class Snippet { 

static HashMap<String,Float> GRADES = new HashMap<String, Float>(12); 

static{ 
    GRADES.put("a", 4f); 
    GRADES.put("a-", 3.7f); 
    GRADES.put("b+", 3.3f); 
    GRADES.put("b", 3f); 
    GRADES.put("b-", 2.7f); 
    GRADES.put("c+", 2.3f); 
    GRADES.put("c", 2f); 
    GRADES.put("c-", 1.7f); 
    GRADES.put("d+", 1.3f); 
    GRADES.put("d", 1f); 
    GRADES.put("d-", 0.7f); 
    GRADES.put("f", 0f); 
} 

public static void main(String[] args) { 
    // Needed variables 
    // ... you codes went here 
} 

,然後簡單地改變方式檢查無效的輸入級用戶到這一點:

if (!GRADES.containsKey(grade)) { 
      JOptionPane.showMessageDialog(null, "Invalid grade entered"); 
     } else { 
      JOptionPane.showMessageDialog(null, "You received " 
        + computeQualityPoints(grade) + " quality points"); 
      number = computeQualityPoints(grade); 
     } 

,它會幫助你的計算number由於這樣的「品位」:

public static float computeQualityPoints(String grade) { 

     Float temp = GRADES.get(grade); 
     if(temp == null){ 
      return 0; 
     } 

     return temp; 
    } 

正如你可能注意到我改變computeQualityPoints的輸出類型從doublefloat,因爲你不需要雙這樣的小浮點值。

如果你不想使用HashMap更改if-else控制結構來switch-case,因爲當你有這種檢查switch-case的優於if-else

希望這會有所幫助。