0

我的代码工作正常,唯一的问题是我的输出不正确,因为我找不到将数字数组设置回零的正确位置。这些程序的基础是接收包含具有相应等级的名称的数据。我的输出应该遵循这个标准:

        Alice [87, 99, 96, 99, 86, 96, 77, 95, 70, 88]
            Name:    Alice
            Length:  10
            Average: 89.30
            Median:  91.5
            Maximum: 99
            Mininum: 70

我得到第一个人的结果是正确的,但是,后面的结果是不正确的,因为它们包含了每个人正在读取的所有值。因此,当我的代码对那个人的数组执行操作时,下一个人将拥有“爱丽丝”的成绩加上他们自己的成绩。我有两个程序,第一个是读取数据并调用方法进行打印和操作的主程序。第二个是具有执行操作的所有方法的类。这是主程序:

    public class Lab2 {

public static void main(String[] args) {

    Scanner in = null; //initialize scanner
    ArrayList<Integer> gradeList = new ArrayList<Integer>(); //initialize gradeList

     //grab data from data.txt 
    try {
        in = new Scanner(new File("data.txt"));
    } catch (FileNotFoundException exception) {
        System.err.println("failed to open data.txt");
        System.exit(1);
    }
    //while loop to grab tokens from data
    while (in.hasNext()) {
        String studentName = in.next();   //name is the first token
        while (in.hasNextInt()) {   //while loop to grab all integer tokens after name
            int grade = in.nextInt();   //grade is next integer token
            gradeList.add(grade);       //adding every grade to gradeList
        }

      //grab all grades in gradeList and put them in an array to work with
        int[] sgrades = new int[gradeList.size()];
        for (int index = 0; index < gradeList.size(); index++) {
            sgrades[index] = gradeList.get(index);  //grade in gradeList put into grades array 
        }

        Grades myGrade = new Grades(studentName,sgrades);
        testGrades(myGrade);
        sgrades = null;


    }


}

public static void testGrades(Grades grades) {
    System.out.println(grades.toString()); 
    System.out.printf("\tName:    %s\n", grades.getName());
    System.out.printf("\tLength:  %d\n", grades.length());
    System.out.printf("\tAverage: %.2f\n", grades.average());
    System.out.printf("\tMedian:  %.1f\n", grades.median());
    System.out.printf("\tMaximum: %d\n", grades.maximum());
    System.out.printf("\tMininum: %d\n", grades.minimum());
    grades = null;
}

      }

我尝试通过将其设置为空来添加位置以擦除下一个人的数组值。我没有运气。

这是包含方法的下一个程序。

    public class Grades {

private String studentName; // name of course this GradeBook represents
private int[] grades; // array of student grades

/**
 * @param studentName The name of the student.
 * @param grades The grades for the student.
 */

public Grades(String name, int[] sgrades) {
    studentName = name; // initialize courseName
    grades = sgrades; // store grades
} // end two-argument GradeBook constructor

/**
 * Method to convert array to a string and print.
 * 
 * @return The name of the student with array of grades.
 */
public String toString() {

    return (String) studentName + " " + Arrays.toString(grades);

}

/**
 * One-argument constructor initializes studentName.
 * The grades array is null.
 * 
 * @param name The name of the student.
 */

public Grades(String name) {
    studentName = name; // initialize courseName
} // end one-argument Grades constructor

/**
 * Method to set the student name.
 * 
 * @return The name of the student.
 */
public String getName() {
    return studentName;
} // end method getCourseName

/**
 * Method to set the length of the amount of grades.
 * 
 * @return Number of grades for student.
 */
public int length() {
    return grades.length; 
}

/**
 * Determine average grade for grades.
 * 
 * @return the average of the grades.
 */
public double average()     {      
    double total = 0; // initialize total
    double average = 0.0;
    // sum grades for one student, while loop
    int index = 0;
    while (index < grades.length) {
        int grade = grades[index];  // get grade at index
        total += grade;
        index++;                    // need to increment
    }
    average = total / grades.length;
    // return average of grades
    return (double) average;
} // end method getAverage


/**
 * Determine median grade for grades.
 * 
 * @return the median of the grades.
 */
public double median()      {

    Arrays.sort(grades);    //sort grades array
    double median = 0.0; 
    if (grades.length%2 == 0) //checks to see if amount of grades is even/odd
        //this is median if list of grades is even
        median = ((double)grades[grades.length/2-1] + (double)grades[grades.length/2])/2;
    else
        //this is median if list of grades is odd
        median = (double) grades[grades.length/2];

    return (double) median;
}

/**
 * Find minimum grade.
 * 
 * @return the minimum grade.
 */
public int minimum() { 
    int lowGrade = grades[0]; // assume grades[0] is smallest

    // loop through grades array, for loop
    for (int index = 0; index < grades.length; index++) {
        int grade = grades[index]; // get grade at index
        // if grade lower than lowGrade, assign it to lowGrade
        if (grade < lowGrade)
            lowGrade = grade; // new lowest grade
    } // end for

    return lowGrade; // return lowest grade
} // end method getMinimum

/**
 * Find maximum grade.
 * 
 * @return the maximum grade.
 */
public int maximum() { 
    int highGrade = grades[0]; // assume grades[0] is largest

    // loop through grades array, for-each loop
    for (int grade : grades) {
        // if grade greater than highGrade, assign it to highGrade
        if (grade > highGrade)
            highGrade = grade; // new highest grade
    } // end for

    return highGrade; // return highest grade
} // end method getMaximum

      }

我的主要问题是,如何为我读到的每个新学生“刷新”数组?

4

1 回答 1

1

你正在寻找gradeList.clear(). 但是你为什么要从gradeListto复制所有内容sgrades?似乎有点多余。

于 2013-09-17T02:01:05.113 回答