我正在尝试编译我的主类,但我的变量平均值似乎没有初始化。我的 Student 类编译没有错误,但我在弄清楚如何让我的主类中的方法正常运行时遇到问题。我在这里做错了什么?任何帮助表示赞赏。
主类:
import javax.swing.JOptionPane;
public class StudentTester {
public static void main(String args[]) {
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
int score1, score2, score3;
int score;
int average;
String firstName;
String lastName;
//start first student here
firstName = JOptionPane.showInputDialog("Enter the student's first name.");
student1.setFirstName(firstName);
lastName = JOptionPane.showInputDialog("Enter the student's last name.");
student1.setLastName(lastName);
JOptionPane.showMessageDialog(null, "Student 1 has name "
+ student1.getFirstName() + ""+ student1.getLastName() + ".");
score = Integer.parseInt(JOptionPane.showInputDialog("Enter the 1st student's 1st score."));
JOptionPane.showMessageDialog(null, "Student 1 has average score " + average);
average = (score1 + score2 + score3);
//start second student here
firstName = JOptionPane.showInputDialog("Enter the student's first name.");
student2.setFirstName(firstName);
lastName = JOptionPane.showInputDialog("Enter the student's last name.");
student2.setLastName(lastName);
JOptionPane.showMessageDialog(null, "Student 2 has name "
+ student2.getFirstName() + "" + student2.getLastName() + ".");
score = Integer.parseInt(JOptionPane.showInputDialog("Enter the 2nd student's 1st score."));
JOptionPane.showMessageDialog(null, "Student 2 has average score " + average);
average = (score1 + score2 + score3);
//start third student here
firstName = JOptionPane.showInputDialog("Enter the student's first name.");
student3.setFirstName(firstName);
lastName = JOptionPane.showInputDialog("Enter the student's last name.");
student3.setLastName(lastName);
JOptionPane.showMessageDialog(null, "Student3 has name "
+ student3.getFirstName() + "" + student3.getLastName() + ".");
score = Integer.parseInt(JOptionPane.showInputDialog("Enter the 3rd student's 1st score."));
JOptionPane.showMessageDialog(null, "Student 3 has average score " + average);
average = (score1 + score2 + score3);
//average score
score = Integer.parseInt(JOptionPane.showInputDialog("Enter the 1st student's score"));
}
}
学生类(class):
public class Student {
private String firstName;
private String lastName;
private int score1, score2, score3;
private int average;
public void setFirstName(String name){
firstName = name;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String name){
lastName = name;
}
public String getLastName(){
return lastName;
}
public void setScore1(String newvalue){
score1 = Integer.parseInt(newvalue);
}
public int getScore1(int newvalue){
return score1;
}
public void setScore2(String newvalue){
score2 = Integer.parseInt(newvalue);
}
public int getScore2(int newvalue){
return score2;
}
public void setScore3(String newvalue){
score3 = Integer.parseInt(newvalue);
}
public int getScore3(int newvalue){
return score3;
}
public int setAverage(int newvalue){
average = (score1 + score2 + score3)/3;
return average;
}
public int getAverage(String newvalue){
return average;
}
}
最佳答案
score = Integer.parseInt(JOptionPane.showInputDialog("Enter the 1st student's 1st score."));
JOptionPane.showMessageDialog(null, "Student 1 has average score " + average);
average = (score1 + score2 + score3);
您尝试在初始化之前在该行中显示平均值
;此外,score1、score2 或score3 从未被初始化。而不是在 StudentTester
中使用变量,请尝试:
主类
student1.setScore1(JOptionPane.showInputDialog("Enter the 1st student's 1st score."));
JOptionPane.showMessageDialog(null, student1.getName() + " has average score " + student1.getAverage();
学生类
public int getAverage()
{
if (score1 == null) score1 = 0;
if (score2 == null) score2 = 0;
if (score3 == null) score3 = 0;
return (score1 + score2 + score3) / 3;
}
public string getName()
{
if (firstName == null) firstName = "";
if (lastName == null) lastName = "";
return firstName + " " + lastName;
}
这会将基于学生的逻辑放入 Student 类中。您可以在此处大大改进简单的空检查,但我正在演示一种更加面向对象的方法。
关于java - 存在明确初始化时显示变量未初始化的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28246106/