2015-11-24 134 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

希望这会有所帮助。