变量变量的数组大小

时间:2013-11-02 02:24:11

标签: java arrays

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

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

课程提供方法:这是我遇到问题的代码......

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

任何帮助将不胜感激!

0 个答案:

没有答案