0
我目前正在研究一種成績冊式的程序。用戶輸入學生的姓名和他們在4次測試中得到的分數,然後輸入2D數組。當我點擊「學生平均」按鈕確定該學生的平均分數時,我希望它打印學生信息,然後再打印出平均分數線。但是,如圖所示,現在的輸出會在顯示平均值之前增加大約15行。JTextArea不斷在for循環中添加行,如何防止這種情況?
我不知道什麼是在我的代碼,是造成這種情況的發生,但我會附上學生平均數按鈕的actionPerformed代碼。此代碼是所有魔術發生:
private void studentAveragesButtonActionPerformed(java.awt.event.ActionEvent evt) {
int sum = 0;
int studentAverage = 0;
studentName = lastNameInput.getText() + " " + firstNameInput.getText();
for (int row = 0; row < studentMarks.length; row++) {
for (int col = 0; col < studentMarks[0].length; col++) {
if (studentMarks[row][col] != null) {
studentMarkScreen.setText(studentMarkScreen.getText() + " " + String.valueOf(studentMarks[row][col]));
if (studentMarks[row][col].equals(studentName)) {
if (studentMarks[row][0].length() > 3) {
for (col = 1; col < studentMarks[0].length; col++) {
sum += Integer.parseInt(studentMarks[row][col]);
}
}
}
} else {
continue;
}
}
}
studentAverage = sum/4;
studentMarkScreen.append("\n");
studentMarkScreen.append("The overall average for " + studentName + " is " + studentAverage + "%.");
}
我強烈建議看看[如何使用表格](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – MadProgrammer