我正在使用此代码以及生成随机数的方法,以使用学生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);
}
}
答案 0 :(得分: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;
}
使用此代码,您可以轻松找到每位学生的所有测验分数的最小值,最大值和平均值。
答案 1 :(得分: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;
}