我無法弄清楚我的程序的問題,非常感謝任何幫助!不幸的是,我是一名初學者程序員......當我運行程序時,它會正確地請求課程數量,學分和成績,但它忽略輸入的學分,並給出了字母等級的正常值。當最後它顯示「你的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;
}
}