我正在尝试创建一个存储学生考试成绩的程序。
每个学生有4个测试分数,必须存储在2D数组中。 JSwing应用程序将允许用户输入4个测试标记,一旦他们单击按钮,它将向阵列添加4个标记。
我一直试图通过逐渐获得arrayindexboundexception来弄清楚如何做到这一点。此外,我不确定我是否正确地做到了。
顺便说一下,这一切都在JSwing
完成。
public class StudentGradesView extends FrameView {
final static int students = 15;
final static int numOfTest = 4;
//DELCARING THE 2D ARRAY
double[][] marks = new double[students][numOfTest];
public void addInfo() {
double testScores[] = new double[4];
String test1, test2, test3, test4;
double score1, score2, score3, score4;
int i = 0;
test1 = testOneIn.getText();
test2 = testTwoIn.getText();
test3 = testThreeIn.getText();
test4 = testFourIn.getText();
score1 = Double.parseDouble(test1);
score2 = Double.parseDouble(test2);
score3 = Double.parseDouble(test3);
score4 = Double.parseDouble(test4);
testScores[0] = score1;
testScores[1] = score2;
testScores[2] = score3;
testScores[3] = score4;
//First for loop starts at in the first row and automatically
//increments to the next row after the first rows columns are filled
for (int row = 0; row < marks.length; row++) {
//Second loop automatically cycles through each column in the
//current row adding test scores. After this loop finishes
//the program returns to the first loop and advances to the next
//row, at which point it will fill that row with the same test data.
for (int col = 0; col < 4; col++) {
marks[row][col] = testScores[i];
i++;
}
}
}
}
这是我到目前为止所拥有的。任何帮助表示赞赏。
答案 0 :(得分:2)
原因问题
for(int row=0; row <marks.length; row++){
for(int col=0;col <4; col++){
marks[row][col]= testScores[i];
i++;
}
增加i超出testScores的长度。使用此
for(int row=0; row <marks.length; row++){
i = 0;
for(int col=0;col <4; col++){
marks[row][col]= testScores[i];
i++;
}
FWIW,您还可以减少本节中的代码量,变量数量,以及最终为变量分配的内存量:
test1=testOneIn.getText();
test2=testTwoIn.getText();
test3=testThreeIn.getText();
test4=testFourIn.getText();
score1=Double.parseDouble(test1);
score2=Double.parseDouble(test2);
score3=Double.parseDouble(test3);
score4=Double.parseDouble(test4);
testScores[0]=score1;
testScores[1]=score2;
testScores[2]=score3;
testScores[3]=score4;
通过做这样的事情
testScores[0] = Double.parseDouble(testOneIn.getText());
答案 1 :(得分:1)
您正在将i
增加到testScores
数组的范围之外。数组的长度只有4. marks
的第二次迭代会导致i > testScores.length
给你异常。
考虑更新您的输入以接受学号:
int student = Integer.parseInt(studentIn.getText());
然后,您只能将测试分数输入应用于该学生:
for (int col = 0, i = 0; col < 4; col++, i++) {
marks[student][col] = testScores[i];
}
此外,由于您没有进行双循环,i
变量不是必需的,您可以使用col
代替:
for (int col = 0; col < 4; col++) {
marks[student][col] = testScores[col];
}
答案 2 :(得分:0)
我不确定是否应该添加第二个答案,但这应该是它的外观
public class StudentGradesView extends FrameView {
final static int students=15;
final static int numOfTest=4;
//Start with the first student in the 2D array
private int row = 0;
//DELCARING THE 2D ARRAY
double[][] marks = new double[students][numOfTest];
public void addInfo(){
double testScores[]=new double[4];
testScores[0]=Double.parseDouble(testOneIn.getText());
testScores[1]=Double.parseDouble(testTwoIn.getText());
testScores[2]=Double.parseDouble(testThreeIn.getText());
testScores[3]=Double.parseDouble(testFourIn.getText());
//Add all four test scores to the current student
for(int col=0;col < testScores.length; col++){
marks[row][col]= testScores[col];
}
//Advance to the next student
row++;
}
}