0

有两个班。1类有方法。类 2 调用方法。我将向您展示第 1 课中我需要帮助的部分,但将包括第 2 课的全部内容,因为它比第 2 课短得多。

1级

public void setHomeworkSum() {
        homeworkSum =+ homeworkScore;
    }

    public int getHomeworkSum() {
        return homeworkSum;
    }

2 级

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;


 public class CourseGrade {

public static void main(String[] args) {


    Student myStudent = new Student();


    myStudent.openFile();

    myStudent.setNumberOfStudents();

    System.out.format("%-10s %25s %15s %10s", "Name", "Assignment score", "Test score", "Grade\n");
    System.out.println();

    for(int i = 1; i <= myStudent.getNumberOfStudents(); i++){

    myStudent.setDefault();

    myStudent.setFirstName();

    myStudent.setLastName();

    myStudent.setHomeworkScore();

    myStudent.setTestScore();

    myStudent.setHomeworkScore();

    myStudent.setTestScore();

    myStudent.computeGrade();


    System.out.format("%-19s %1s %21s %13s", myStudent.getFirstName() + " " +      myStudent.getLastName(), myStudent.getHomeworkScore(), myStudent.getTestScore(), myStudent.getGrade() +"\n");

    myStudent.setPassFail();

    myStudent.setHomeworkSum();

    }

    System.out.println();
    System.out.println("No. of students passed: " + myStudent.getPass());
    System.out.println();
    System.out.println("No. of students failed: " + myStudent.getFail());
    System.out.println();
    System.out.println("sum: " + myStudent.getHomeworkSum());

}

}

基本上,我遇到的问题是它不会返回所有作业分数的总和,而是返回已读取的最新个人作业分数。

Output:

Name                Assignment score      Test score     Grade

Anthony Hopkins     854                   284            G
John Smith          730                   214            A
Pan Mei             730                   267            A
Rafael Vega         801                   236            A

No. of students passed: 4

No. of students failed: 0

mean 801

(顶部没有正确转换,但你明白了。)

4

1 回答 1

4

改成

homeworkSum += homeworkScore;

你有过

homeworkSum =+ homeworkScore;

其中,如果你放一些空间,基本上是

homeworkSum = +homeworkScore;

所以你总是将最后一个值分配给变量。

于 2013-10-19T21:37:14.507 回答