显示清除初始化时未初始化变量的错误

时间:2015-01-30 23:07:08

标签: java variables methods bluej

我正在尝试编译我的主类,但我的变量平均值似乎没有被初始化。我的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"));



        }
    }

学生班:

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;
} 
}

1 个答案:

答案 0 :(得分:1)

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);

您尝试在初始化之前在行中显示average;此外,在任何时候都没有得分,得分2或得分3已初始化。而不是在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类中。你可以在这里对简单的空值检查进行大量改进,但是我正在展示一种更加面向对象的方法。