Java GPA计算问题
我的总数不会更新。每次我执行代码时,gpa显示0.0。我添加了gp,以查看在输入用户信息时,分数是否会更新,但不会更新。任何帮助都会很好!我看了其他问题,似乎无法解决我的问题Java GPA计算问题,java,Java,我的总数不会更新。每次我执行代码时,gpa显示0.0。我添加了gp,以查看在输入用户信息时,分数是否会更新,但不会更新。任何帮助都会很好!我看了其他问题,似乎无法解决我的问题 import javax.swing.JOptionPane; public class GUITestClient { public static void main(String[] args) { StudentInfo student = new StudentInfo();
import javax.swing.JOptionPane;
public class GUITestClient {
public static void main(String[] args) {
StudentInfo student = new StudentInfo();
double credits;
String name = JOptionPane.showInputDialog("Please enter your name:");
student.setName(name);
credits = Double.parseDouble(JOptionPane.showInputDialog("Please enter the credits:"));
student.setCredits(credits);
String grade = JOptionPane.showInputDialog("Please enter your grade:");
student.setGrade(grade);
JOptionPane.showMessageDialog(null, student.displayStudentInformation());
}
}
public 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 double addClass(double totalGradePoints, String grade){
double gradePoints = 0;
if(grade.equals("A")){
gradePoints = 4.0;
}else if(grade.equals("B")){
gradePoints = 3.0;
} else if(grade.equals("C")){
gradePoints = 2.0;
} else if(grade.equals("D")){
gradePoints = 1.0;}
totalGradePoints = (totalGradePoints +gradePoints);
return getGradePoints();
}
public double getGPA(){
this.setGpa(this.getCredits() / this.getGradePoints());
return this.getGpa();
}
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() + "\n";
output = output + "Press any key to continue!" + "\n";
output = output + "gp" + totalGradePoints + "\n";
return output;
}
}
在addClass方法中,将totalGradePoints作为局部变量
public double addClass(double totalGradePoints, String grade)
您应该使用全局变量totalGradePoints,而不是局部变量。因此,请删除该变量,使该方法看起来像
public double addClass(String grade)
但是我没有看到任何地方可以调用addClass方法
public double addClass(double totalGradePoints, String grade){
double gradePoints = 0;
if(grade.equals("A")){
gradePoints = 4.0;
}else if(grade.equals("B")){
gradePoints = 3.0;
} else if(grade.equals("C")){
gradePoints = 2.0;
} else if(grade.equals("D")){
gradePoints = 1.0;}
totalGradePoints = (totalGradePoints +gradePoints);
}
问题在addClass的最后一个语句中。Java是按值传递的,这意味着此处可见的totalGradePoints是addClass方法中的一个局部变量,包含调用addClass时传递的任何值的副本。对该值所做的任何更新仅影响本地副本,而不影响原始变量
方法签名不需要totalGradePoints参数。应该是
public double addClass(String grade) {
...
您需要将本地成绩点添加到类的成员变量totalGradePoints。问题在于您的this.getGradePoints。它不是一个值的getter,并且您不在函数中为StudentInfo student=new StudentInfo中的学生的同一对象实例设置值; 您必须在创建的对象“student”上设置所有setter变量 试试这个:
package guitestclient;
import javax.swing.JOptionPane;
public class GUITestClient {
public static void main(String[] args) {
StudentInfo student = new StudentInfo();
double credits;
double gradePoints = 0;
double gradePointsTot = 0;
double gpa = 0;
int classCount = 0;
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);
//calculates gpa value for grade
gradePoints = StudentInfo.addClass(gradePoints, grade);
gradePointsTot += gradePoints;
classCount++;
} while (classCount < 5);
//after loop
student.setGradePoints(gradePointsTot);
gpa = StudentInfo.getGPA(credits, gpa, classCount);
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;
if(grade.equals("A")){
gradePoints = 4.0;
}else if(grade.equals("B")){
gradePoints = 3.0;
} else if(grade.equals("C")){
gradePoints = 2.0;
} else if(grade.equals("D")){
gradePoints = 1.0;}
totalGradePoints = (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() + "\n";
output = output + "Press any key to continue!" + "\n";
output = output + "gp" + this.getGradePoints() + "\n";
return output;z
}
}
是的,这将是一个多个班级/年级项目的循环,但我想确保我能首先得到一个。我按照您的指示进行了操作,但它们仍然不会根据用户输入进行更新。我不确定我是否理解这会是什么样子?获得/设置分数?我不仅想现在就拥抱你,我还想开始跑步,跳起来,用腿搂住你,拥抱你直到感觉奇怪。这不是100%正确,但它让我达到了我需要的地方。这里是唯一的更正:public static double getGPAdouble totalGradePoints,double credits,double gpa{gpa=credits*totalGradePoints/credits;return gpa;}您还应该检查已接受的答案是否对您的问题找到解决方案最有帮助:如果我知道答案的位置,我会这样做。你不会碰巧知道如何在do while循环中添加累加器来累加变量Credit和totalGradePoints吧?它就在答案左侧的upvote/downvote箭头的正下方