Java 如何列出个别学生的成绩。。。?
使用多维数组对我来说是第一次,所以我对这些数组的一些方面的理解有点…模糊。现在快速回顾一下这个程序完成后将用于什么Java 如何列出个别学生的成绩。。。?,java,arrays,loops,awt,Java,Arrays,Loops,Awt,使用多维数组对我来说是第一次,所以我对这些数组的一些方面的理解有点…模糊。现在快速回顾一下这个程序完成后将用于什么 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
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) {
它收集输入的学生姓名和四个相应的考试分数。你可以用它找到学校的平均水平,现在我试图找到每个学生的平均水平
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) {
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) {
//这很好…只需收集姓名和考试成绩并将其列在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) {
aryStudent[numOfStudents][0]=Integer.parseInt(test1Field.getText());
aryStudent[numOfStudents][1]=Integer.parseInt(test2Field.getText());
aryStudent[numOfStudents][2]=Integer.parseInt(test3Field.getText());
arystustudent[numOfStudents][3]=Integer.parseInt(test4Field.getText());
StringBuilder sb=新的StringBuilder();
对于(int x=0;x最后一段代码中的变量marks
到底是什么?它在循环之外,因此实际上什么都不做。例如,如果有4个学生,且分数为3,我希望代码执行以下操作
(i=0 y=0),学生A
(i=0 y=1),学生b
(i=0 y=2),学生C
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) {
(i=1 y=0), StudentA <AVG>
(i=1 y=1), StudentB <AVG>
(i=1 y=2), StudentC <AVG>
(i=2 y=0), StudentA <AVG>
(i=2 y=1), StudentB <AVG>
(i=2 y=2), StudentC <AVG>
....
(i=1 y=0),学生A
(i=1 y=1),学生B
(i=1 y=2),学生C
(i=2 y=0),学生A
(i=2 y=1),学生B
(i=2 y=2),学生C
....
首先尝试以文本模式打印结果(无任何GUI)。当结果在那里运行时,您可以移动到GUI。您的索引似乎不正确。您使用的是y
,它是作为学生索引的标记索引,i
未使用。此外,您这样做的方式不需要两个循环
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) {
如果您为学生命名为i
s
,为马克命名为y
m
,这个问题可能会避免
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) {
另外,通过使用setText
,您正在替换文本,而不是添加到文本中。假设您使用的是JTextArea
,则应该使用append
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) {
它应该是这样的:
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) {
averageField.setText(""); // clear
for (int s = 0; s < numOfStudents; s++)
averageField.append(studentNames[s] + " - " + (aryStudent[s][0] +
aryStudent[s][1] + aryStudent[s][2] + aryStudent[s][3])/4 + "\n");
另外,学生的numOfStudents
是否会(或不应该)限于15
,为什么要同时检查这两个值?平均字段的类型是什么?
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) {