1

So first off, sorry if this appears if I am asking you to do my homework...just me you are not, this question only involves one tiny little aspect of the entire program that is in the making...In other words this isn't close to be finished.

I would ask my teacher, but its an online course. So then I would ask my friends for advice, but I'm the only kid in my school taking this computer science course. (It's a small school)

Anyways this program when completed, will allow a teacher to input a list of students of four grades per student. Eventually I'll add some code that allows the teacher to find the averages of each student etc etc.

However at the moment I'm only in the process of listing students. Where there is a small problem...

Every time I list a student (their last and first name) along with the four test scores, it all outputs nicely. However when I go to output the second student with a different name and different test scores, it outs the different name...but the test scores of the previous student.

Ex.

John Smith: 67 68 57 87

Jane Doe: 67 68 57 87 (even though it should Jane Doe: 54 68 75 91)

And clearly that is a problem...since all students do not have the exact same grades...ever..

Okay code below...

public class StudentGradesView extends FrameView {
    int [][] aryStudent = new int [15][4]; //[15] being number of students and [4] being test scores...
    String[] studentNames = new String[15]; // I'm thinking this is might unnecessary now...but we'll see
    int numOfStudents = 0; //starting number of students at 0....

    int marks = 0;

    int test1;
    int test2;
    int test3;
    int test4;

    public StudentGradesView(SingleFrameApplication app) {
        //unimportant...just stuff added for GUI..
    }

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
        //test1Field through test4Field are the test scores....
        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++) {
            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 ++;
    }                                        
}
4

1 回答 1

0

First of all, you want

for (int x=0; x<= numOfStudents; x++)

or when numOfStudents = 0 you'll never go into the loop.

Also, does addButtonActionPerformed have a for loop that contains the code you've given us? Otherwise, it seems like every time you go into that method numOfStudents will be 0 and when you exit it'll be 1, but the code is only executed for numOfStudents = 0. Am I missing something?

于 2013-04-25T15:45:40.393 回答