-1

我正在使用此代码以及一种生成随机数的方法,以用学生 ID 和测验分数填充我的数组。接下来我想找到所有测验分数的平均值/最小值/最大值。我真的被困住了。任何和所有的帮助表示赞赏。

private class FillButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        int quizScore, firstIDin = firstID, row, column;
        int[] studentIDsin = new int[numberStudents];
        int[][] quizin = new int[numberStudents][numberQuizes];

        for(row=0; row<numberStudents; row++) {
            studentIDsin[row] = firstIDin;
            firstIDin+=1;
        }
        for(row=0; row<numberStudents; row++) {
            studentIDs[row] = studentIDsin[row];
        }
        for(row=0; row<numberStudents; row++) {
            for(column=0; column<numberQuizes;column++) {
                quizScore = (int) rand();
                quizin[row][column]= quizScore;
            }
        }
        for(row=0; row<numberStudents; row++) {
            for(column=0; column<numberQuizes;column++)
                quiz[row][column]= quizin[row][column];
        }
        dispResultsMenuPriv.setEnabled(true);
    }

}

4

2 回答 2

0
for(row=0; row<numberStudents; row++) {
    int min = 0, max = 0;
    float average = 0.0f;
    for(column=0; column<numberQuizes;column++) {
        average += quizin[row][column];
        min = (quizin[row][column] < min) ? quizin[row][column] : min;
        max = (quizin[row][column] > max) ? quizin[row][column] : max;
    }
    average = average/numberQuizzes;
}

使用此代码,您可以轻松找到每个学生的所有测验分数的最小值、最大值和平均值。

于 2013-04-28T06:08:24.373 回答
0

尝试这个

    double[] avg = new double[numberQuizes];
    int[] min = new int[numberQuizes];
    int[] max = new int[numberQuizes];

    // for each student
    for (int[] a : quizin) {
        for (int i = 0; i < numberQuizes; i++) {
            int score = a[i];
            avg[i] += score;
            if (min[i] > score) {
                min[i] = score;
            }
            if (max[i] < score) {
                max[i] = score;
            }
        }
    }
    for (int i = 0; i < numberQuizes; i++) {
        avg[i] /= numberStudents;
    }
于 2013-04-28T06:09:22.520 回答