使用多维数组对我来说是第一次,所以我对这些数组附带的一些方面的理解有点……模糊。现在快速回顾一下这个程序在完成后将用于什么......
它将允许教师输入学生的姓名和四个相应的考试成绩。他们最多可以列出 15 名学生。
前任。约翰·史密斯:67 75 84 52 简·多伊:54 86 81 79
填写完 firstNameField、lastNameField 和 test1Field-test4Field 后,按 addButton 将其输出到另一个字段并输出您添加的每个学生,创建一个列表。
我的问题是找到班级平均水平。我想找出每个学生的四个考试成绩(而不是名字),在那里我会创建一个方程来找到平均值,这很容易。问题是,我不确定谁可以从数组中提取所有测试分数,所以我可以这样做......
public class StudentGradesView extends FrameView {
int [][] aryStudent = new int [15][4]; // [15] being # of students, [4] for test scores
String[] studentNames = new String[15]; //this isn't being used now...
int numOfStudents = 0; //listing the students starts off from 0...
int marks = 0; //might be needed for grabbing out test scores for finding average?
int test1;
int test2;
int test3;
int test4;
public StudentGradesView(SingleFrameApplication app) {
//unimportant...for GUI...
}
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
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();
//The following is the code that list the students and their grades...this code should be fine..
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]);
// studentListField.setText("" + firstNameField.getText() + " " + lastNameField.getText() + " " + aryStudent[numOfStudents][y] + "\n" + currentList);
}
sb.append("\n");
}
studentListField.setText(sb.toString());
numOfStudents ++;
}
private void classAverageButtonActionPerformed(java.awt.event.ActionEvent evt) {
“int schoolAverage = (test1 + test2 + test3 + test4)/4;” //这是我希望的工作......但之后输出总是为零。所以我想我没有正确地将测试分数从数组中拉出来。所以我尝试了一个类似于我上面所做的循环......截至目前我迷失在黑暗中。
for (int x=0; x <= numOfStudents && x<15; x++) {
for (int y=0; y < marks && y <4; y++) {
averageField.setText("" + /** I'm unsure what to do here */ );
}
}
numOfStudents ++;
marks++;
// studentListField.getText(averageField.setText(""));
}