我的总数不会更新。每次执行代码时,gpa都会显示0.0。我添加了" gp"查看输入用户信息时是否会更新"成绩点,但不会。任何帮助都会很棒!我看了看其他问题,似乎无法解决我的问题!
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;
}
}
答案 0 :(得分:0)
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
方法中的局部变量,包含 副本< / em> 您调用addClass
时传递的任何值。您对该值所做的任何更新仅影响本地副本,而不影响原始变量。
方法签名不需要totalGradePoints
参数。它应该是
public double addClass(String grade) {
...
您需要将本地gradePoints
添加到班级&#39;成员变量totalGradePoints
。
答案 1 :(得分:0)
问题在于你的this.getGradePoints()。它不是值的getter,并且您没有在StudentInfo student = new StudentInfo()中为学生的同一对象实例设置函数内部的值; 您必须在该对象上设置所有setter变量&#39; student&#39;你创造了。
试试这个:
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
}
}
答案 2 :(得分:-1)
在你的addClass方法中,你有totalGradePoints作为局部变量。
public double addClass(double totalGradePoints, String grade)
您应该使用全局变量totalGradePoints而不是本地变量。因此,删除该变量,使方法看起来像
public double addClass(String grade)
但我没有看到你调用addClass方法的任何地方。