我正在尝试使用带有未知条目集的while循环制作GPA计算器。当只输入一组信息时,我的程序运行正常。如果我输入多个班级的信息,总数不会更新,我无法弄清楚原因。有任何想法吗?
import javax.swing.JOptionPane;
public class GUITestClient {
public static void main(String[] args) {
StudentInfo student = new StudentInfo();
double credits = 0;
double gradePoints = 0;
double gradePointsTot = 0;
double gpa = 0;
String addAnotherClass = null;
String name = JOptionPane.showInputDialog("Please enter your name:");
student.setName(name);
do{
credits = Double.parseDouble(JOptionPane.showInputDialog("Please
enter the credits:"));
student.setCredits(credits);
String grade = JOptionPane.showInputDialog("Please enter your
grade:");
student.setGrade(grade);
addAnotherClass = JOptionPane.showInputDialog("Press "+"Y"+" to
enter More class information");
gradePoints = StudentInfo.addClass(gradePoints, grade);
gradePointsTot += gradePoints;
}while(addAnotherClass.equalsIgnoreCase("y"));
//after loop
student.setGradePoints(gradePointsTot);
StudentInfo.getGPA(credits, gpa, gradePointsTot);
student.setGpa(gpa);
JOptionPane.showMessageDialog(null,
student.displayStudentInformation());
}
}
class StudentInfo {
private String name;
private double totalGradePoints;
private double credits;
private String grade;
private double gpa;
public StudentInfo(){
setGrade(null);
setCredits(0);
setGradePoints(0);
}
public StudentInfo(double credits, double totalGradePoints, String
grade){
setGrade(grade);
setCredits(credits);
setGradePoints(totalGradePoints);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public double getCredits() {
return credits;
}
public void setCredits(double credits) {
this.credits = credits;
}
public double getGradePoints() {
return totalGradePoints;
}
public void setGradePoints(double totalGradePoints) {
this.totalGradePoints = totalGradePoints;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public static double addClass(double totalGradePoints, String grade){
double gradePoints = 0;
double accumGradePoints;
if(grade.equalsIgnoreCase("A")){
gradePoints = 4.0;
}else if(grade.equalsIgnoreCase("B")){
gradePoints = 3.0;
} else if(grade.equalsIgnoreCase("C")){
gradePoints = 2.0;
} else if(grade.equalsIgnoreCase("D")){
gradePoints = 1.0;}
totalGradePoints += gradePoints;
return totalGradePoints;
}
public static double getGPA(double totalGradePoints, double credits,
double gpa){
gpa = (credits * totalGradePoints)/ credits);
return gpa;
}
public String displayStudentInformation(){
String output = "";
output = output + "Name: " + this.getName() + "\n";
output = output + "Total Credits: " + this.getCredits() + "\n";
output = output + "Your grade is: " + this.getGrade() + "\n";
output = output + "Your GPA is:
" + this.getGPA(totalGradePoints, credits, gpa) + "\n";
output = output + "Press any key to continue!" + "\n";
output = output + "gp" + totalGradePoints + "credits" + credits;
return output;
}
}