gpt4 book ai didi

Java GPA CALC 问题

转载 作者:行者123 更新时间:2023-12-01 06:10:05 25 4
gpt4 key购买 nike

我的总计不会更新。每次我执行代码时 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;
}
}

最佳答案

问题出在你的 this.getGradePoints() 上。它不是值的 getter,并且您不会在函数内为来自 StudentInfo Student = new StudentInfo(); 的学生的同一对象实例设置值;您必须在您创建的对象“学生”上设置所有 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
}
}

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

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