1

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.

2
  • wouldn't it be wise to take the print statement out of your for loop? Commented May 7, 2019 at 22:12
  • also what is tests? An array? Commented May 7, 2019 at 22:13

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

tests[0] should be tests[n], whether [m] is present or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.