0

我的问题是我将数组设置为可变变量的大小。当变量更改时,先前存储在数组中的值会丢失并重置为 0。这是我的代码:

有学生方法,这里不重要。基本上它会扫描 .txt 文件中的变量并将它们设置为代码中的变量。

courseoffering方法:这是我遇到问题的代码...

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

public class CourseOffering {

    Student myStudent = new Student();

    private Scanner scanner;


//initialize data members
    private int students;
    private String description;
    private String fallSpring;
    private String year;
    private double allTotalHomeworkScore = 0;
    private int allTotalTestScore = 0;
    private int totalStudents = 0;
    private int numberOfSemesters;
    private int studentArrayCount = 0;
    private int homeworkArrayCount = 0;
    private double homeworkAverage;
    private double testAverage;
    private int failingStudents;
    private int passingStudents;
    private int computeHomeworkCounter;
    private int computeTestCounter;
    private int computeHomeworkDeviationCounter = 0;
    private double homeworkDeviation;
    private double top;

    private int totalHomework;
    int l = 0;
    int a = 0;

    int[] homeworkScore = null;

    public void setScanner(Scanner scanner){
    this.scanner = scanner;
}

public void setNumberOfSemesters(){
    if(scanner.hasNext()){
        numberOfSemesters = scanner.nextInt();
    }
}

public int getNumberOfSemesters(){
    return numberOfSemesters;
}

public void setStudents(){
    int[] student = new int[numberOfSemesters];
    student[studentArrayCount] = scanner.nextInt();
    students = student[studentArrayCount];
    studentArrayCount++;
    totalStudents += students;

    homeworkScore = new int[totalStudents];     
}

 public void setHomeworkArray(int homework){
    this.homeworkScore[this.l] = homework;
    System.out.println(homeworkScore[this.l]);
    this.l++;
}

主要方法(这是数组内部变量更改的地方):

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

public class CourseStatistics {

    public static void main(String[] args) {

    Scanner scanner = null;

    try { //start try statement
        scanner = new Scanner(new File("gradesA5.txt")); //initialize scanner to scan from the grades.txt file
    } //end try statement
    catch (FileNotFoundException e) { //start catch statement
        System.out.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class"); //print statement telling user that the grades.txt file is not in the right place
        System.exit(0); //system exit 
    } //end catch statement

    Student myStudent = new Student();

    CourseOffering myCourse = new CourseOffering();     

    myStudent.setScanner(scanner);

    myCourse.setScanner(scanner);

    myCourse.setNumberOfSemesters();

    for(int i = 0; i < myCourse.getNumberOfSemesters(); i++){

        myCourse.setDescription();

        myCourse.setStudents();

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

        for(int j = 0; j < myCourse.getStudents(); j++){

        myStudent.setfirstName();

        myStudent.setLastName();

        myStudent.setAssignmentScore();

        myStudent.setLabScore();

        myStudent.setMidterm1Score();

        myStudent.setMidterm2Score();

        myStudent.setFinalExamScore();

        myStudent.setQuizScore();

        myStudent.setAttendanceScore();

        myStudent.setPATScore();

        myStudent.setZyanteScore();

        myStudent.computeLetterGrade();

        myCourse.computeHomeworkAverage(myStudent.computeTotalHomeworkScore());

        myCourse.computeTestAverage(myStudent.computeTotalTestScore());

        myCourse.setHomeworkArray(myStudent.computeTotalHomeworkScore());                       

        if(myStudent.computeTotalTestScore() >= 100)
        {
        System.out.format("%-19s %1s %21s %13s", myStudent.getFirstName() + " " + myStudent.getLastName(), myStudent.computeTotalHomeworkScore(), myStudent.computeTotalTestScore(), myStudent.getLetterGrade() +"\n");
        }

        else
        { 
            System.out.format("%-19s %1s %20s %14s", myStudent.getFirstName() + " " + myStudent.getLastName(), myStudent.computeTotalHomeworkScore(), myStudent.computeTotalTestScore(), myStudent.getLetterGrade() +"\n");
        }

        }
        System.out.println();
    }

任何帮助,将不胜感激!

4

0 回答 0