我无法弄清楚我的程序的问题,非常感谢任何帮助!不幸的是,我是一名初学程序员...当我运行该程序时,它会正确地询问课程,学分和成绩的数量,但它忽略了输入的学分,只是给出了字母等级的正常值。最后它说"你的GPA是0.0"当显然是不正确的。再次感谢!
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;
}
}
答案 0 :(得分:1)
totalCreditsEarned += (credits * number);
number
仍然是0.0
,这意味着totalCreditsEarned
和gpa
也将保持0.0
。
我怀疑
computeQualityPoints(grade);
你忽略了返回的值,应该是
number = computeQualityPoints(grade);
(至少我假设number
应该包含的内容)
答案 1 :(得分: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'
课程学分:
credits = Integer.parseInt(JOptionPane.showInputDialog(null, "How many credit was "+(count+1)+"'th class?"));
这为您的用户提供了更好的体验。
此外,您可以通过将其存储在地图结构(例如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
由于等级&#39;像这样:
public static float computeQualityPoints(String grade) {
Float temp = GRADES.get(grade);
if(temp == null){
return 0;
}
return temp;
}
您可能已经注意到我将computeQualityPoints
的输出类型从double
更改为float
,因为您不需要为这么小的浮点值加倍。< / p>
如果您不想使用HashMap
将if-else
控件结构更改为switch-case
,因为当您进行此类检查时,switch-case
优先于if-else
。
希望这会有所帮助。