DonFluffles
DonFluffles

Reputation: 19

Print Statement in Loop Repeats Multiple Times

I'm working on code that inputs a multiple choice test in a 2D array, checks the answers with a guide array, and tells the student how many problems they get correct and incorrect. I can check the answers for all of the students, but the statement that gives the number of correct and incorrect questions gets printed 8 times (the width of the 2D array) instead of once.

I've tried playing with the values in the nested for loop for how the parameters I've checked.

for (int n = 0; n < tests.length; n++) {
    for (int m = 0; m < tests[0].length + 1; m++) {

        if (m < tests[0].length) {
            if (!(tests[0][m].equals(answers[m]))) {
                incorrect++;
            } else if (tests[0][m].equals(answers[m]))
                correct++;
        } else {
            System.out.println(
                "You got " + correct + " answers correct and " + incorrect + " answers wrong.");
            correct = 0;
            incorrect = 0;
        }
    }
}

I expect to just have the print statement printed once per student instead of 8 times which is what occurs.

Upvotes: 1

Views: 57

Answers (1)

joshua0823
joshua0823

Reputation: 390

it looks like all your tests[0][m] should be tests[n][m] unless I'm missing something here. Otherwise it just loops through the same test over and over

Upvotes: 3

Related Questions