gpt4 book ai didi

Java GPA 计算器问题

转载 作者:行者123 更新时间:2023-12-01 10:51:14 24 4
gpt4 key购买 nike

我无法找出我的程序的问题,非常感谢任何帮助!不幸的是,我是一名初学者程序员......当我运行该程序时,它会正确要求类(class)数量、学分和成绩,但它会忽略输入的学分,只给出字母成绩的正常值。最后它还写着“你的 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;
}
}

最佳答案

totalCreditsEarned += (credits * number);

number 仍为 0.0,这意味着 totalCreditsEarnedgpa 也将保持为 0.0 >.

我怀疑

computeQualityPoints(grade);

您忽略返回值的地方应该是

number = computeQualityPoints(grade);

(至少我假设这就是 number 应该包含的内容)

关于Java GPA 计算器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com