Java 构造函数类出错?

Java 构造函数类出错?,java,constructor,Java,Constructor,我写了一节课和一个程序,为一个特定的学生生成测验平均值 我不确定主类或StudentClass 1中是否有错误,但每次运行程序时,它输出的两个值都是0 有人能看到导致这种情况的代码问题吗?这是主要课程: public class Student { public static void main(String[] args) { StudentClass quizzes = new StudentClass(); int[] quizArray = {54, 85, 32,

我写了一节课和一个程序,为一个特定的学生生成测验平均值

我不确定主类或StudentClass 1中是否有错误,但每次运行程序时,它输出的两个值都是0

有人能看到导致这种情况的代码问题吗?这是主要课程:

public class Student {

public static void main(String[] args) {
    StudentClass quizzes = new StudentClass();

    int[] quizArray = {54, 85, 32, 98, 43, 89};

    quizzes.addQuiz(quizArray[0]);
    quizzes.addQuiz(quizArray[1]);
    quizzes.addQuiz(quizArray[2]);
    quizzes.addQuiz(quizArray[3]);
    quizzes.addQuiz(quizArray[4]);
    quizzes.addQuiz(quizArray[5]);

    int total = quizzes.getTotalScore();
    int average = quizzes.getQuizAverage();

    System.out.println("Total score: " + total);
    System.out.println("Quiz average: " + average);



        }

    }
这是学生班:

public class StudentClass {
private String name;
private static int numberQuizzes;
private int average;
private int score;
private int total;

public String getName(String name) {
    return this.name;

}

public int addQuiz(int score){
    numberQuizzes++;
    return score;
}

public int getTotalScore(){
    total += score;
    return total;

}

public int getQuizAverage(){
    average = total / numberQuizzes;
    return average;

}

}

您需要在
addquick
中添加测验croE,而不是
getTotalScore
方法。移动此行:

 total += score;

这样,您将计算每个添加的测验的总数。

您需要在
addquick
中添加测验croE,而不是
getTotalScore
方法。移动此行:

 total += score;

通过这种方式,您将计算每个添加的测验的总数。

addquick方法似乎对分数没有任何影响(例如将其添加到总数中)

getTotalScore()似乎也对“分数”是什么感到困惑。我不认为您希望将“total”类型的方法添加到任何成员/字段中,而只是返回total

未经测试,但请尝试:

public int addQuiz(int score){
    numberQuizzes++;
    total += score;
    return score;
}

public int getTotalScore(){
    return total;

}

addquick方法似乎对分数没有任何影响(例如将其添加到总数中)

getTotalScore()似乎也对“分数”是什么感到困惑。我不认为您希望将“total”类型的方法添加到任何成员/字段中,而只是返回total

未经测试,但请尝试:

public int addQuiz(int score){
    numberQuizzes++;
    total += score;
    return score;
}

public int getTotalScore(){
    return total;

}
您使用错误的方法放置了“total+=score;”。因此,学生类中的内部值永远不会更新

记住初始化类。
我在代码中添加了一些注释

public class StudentClass {
private String name;
private static int numberQuizzes =0 ; //increment this every time addQuiz is called
// private int average; // you do not need this...you can re-calculate this
// private int score; //you do not need this.
private int total =0 ; //update your total score on addQuiz()

public int addQuiz(int score){
    numberQuizzes++;
    //update the total score everytime addQuiz() is called.
    total+= score; // this line is missing in your codes.

    return score;
}

public int getTotalScore(){
    // total += score; // this line is redundant.
    return total; 
}

public int getQuizAverage(){
    if(numberQuizzes==0) return 0; //prevent div by zero exception

    return total / numberQuizzes; 

}
您使用错误的方法放置了“total+=score;”。因此,学生类中的内部值永远不会更新

记住初始化类。
我在代码中添加了一些注释

public class StudentClass {
private String name;
private static int numberQuizzes =0 ; //increment this every time addQuiz is called
// private int average; // you do not need this...you can re-calculate this
// private int score; //you do not need this.
private int total =0 ; //update your total score on addQuiz()

public int addQuiz(int score){
    numberQuizzes++;
    //update the total score everytime addQuiz() is called.
    total+= score; // this line is missing in your codes.

    return score;
}

public int getTotalScore(){
    // total += score; // this line is redundant.
    return total; 
}

public int getQuizAverage(){
    if(numberQuizzes==0) return 0; //prevent div by zero exception

    return total / numberQuizzes; 

}

非常感谢你@如果答案有帮助,你可以接受。再见,非常感谢@如果答案有帮助,你可以接受。看见