使用多维数组对我来说是第一次,所以我对这些数组附带的一些方面的理解有点……模糊。现在快速回顾一下这个程序在完成后将用于什么......
它收集输入的学生姓名和四个相应的考试成绩。你可以用它找到学校的平均值,现在我正试图找到每个学生的平均值。
public class StudentGradesView extends FrameView {
int [][] aryStudent = new int [15][4]; // [15] being max students [4] being # of test scores
String[] studentNames = new String[15];
int numOfStudents = 0; //student names start from 0...
int marks = 0;
int test1;
int test2;
int test3;
int test4;
public StudentGradesView(SingleFrameApplication app) {
//unimportant...for GUI
}
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
//这很好...只是收集姓名和考试成绩并将它们保存在studentListField中...
aryStudent[numOfStudents][0] = Integer.parseInt(test1Field.getText());
aryStudent[numOfStudents][1] = Integer.parseInt(test2Field.getText());
aryStudent[numOfStudents][2] = Integer.parseInt(test3Field.getText());
aryStudent[numOfStudents][3] = Integer.parseInt(test4Field.getText());
StringBuilder sb = new StringBuilder();
for (int x=0; x <= numOfStudents && x<15; x++) {
sb.append(firstNameField.getText() + " " + lastNameField.getText());
for (int y=0; y < 4; y++) {
sb.append(" " + aryStudent[x][y]);
}
sb.append("\n");
}
studentListField.setText(sb.toString());
numOfStudents ++;
}
private void classAverageButtonActionPerformed(java.awt.event.ActionEvent evt) {
//找到学校平均水平....很好
for(int x = 0; x < numOfStudents && x<15; x++) {
averageField.setText("The class average is " + (aryStudent[x][0] + aryStudent[x][1] + aryStudent[x][2] + aryStudent[x][3])/4 + "%");
}
}
private void studentAverageButtonActionPerformed(java.awt.event.ActionEvent evt) {
但是之后!这……我以为这会单独列出学生的平均成绩,但它一次只输入一个学生,这是一个问题……它需要做的就是列出与此类似的列表……
John Smith 78 Jane Doe 80 等
for (int i = 0; i < numOfStudents; i++) {
for (int y = 0; y < marks; y++) {
averageField.setText("Student" + (aryStudent[y][0] + aryStudent[y][1] + aryStudent[y][2] + aryStudent[y][3])/4);
}
}
marks++;
}