1

I am working on a piece of code which is used to create a multiple choice question exam. The purpose of the code I am working on is to enable a question to have multiple correct answers.

The way a user inputs an answer is choosing an integer corresponding to the choice they would like to submit.

The actual correct integer(s) is/are stored in an array called "correctedIndex" and the user stored responses are stored in an array called "response".

The error I am having is checking to see whether the user's answers are correct, regardless of the order he or she submitted them in. My current idea was to created a nested for loop to cycle through both arrays and check for matches, and if the matches equal to the amount of correct answers, then the student would be correct. This does not work when I run it.

Everything before that point works properly, I have made sure a user cannot enter invalid responses, duplicate responses, and automatically mark them incorrect if they submit more responses than there are correct responses.

Here is the part of the code which does not work:

for(int i = 0; i < correctIndex.length; i++){
            for(int j = 0; j < response.length; j++){
                if(correctIndex[i] == response[j]){
                    matches++;
                }
            }
        }
        if(matches == correctIndex.length){
            System.out.println("Correct, answer was: ");
            for(int i = 0; i < correctIndex.length; i++){
                System.out.println((correctIndex[i] + 1) + ": " + choices[correctIndex[i]]);
            }
        }
        else{
            System.out.println("Your answer: ");
            for(int i = 0; i < response.length; i++){
                System.out.println(response[i] + ": " + choices[response[i]-1]);
            }
            System.out.println();
            System.out.println();
            System.out.println("Is incorrect, the correct answer is: ");
            for(int i = 0; i < correctIndex.length; i++){
                System.out.println((correctIndex[i] + 1) +": " + choices[correctIndex[i]]);
            }
        }

Here is an example of an output I get:

Please input the correct choice using its corresponding number

Which of these are visibilities?

1: Public
2: Private
3: Void
4: Protected
5: Int
6: String
7: Package-Protected

Please enter how many answers you would like to select: 
4
Enter your number for answer 1
1
Enter your number for answer 2
2
Enter your number for answer 3
4
Enter your number for answer 4
7

And here are the results after using the result method (The one in question):

Question: 
Which of these are visibilities?

Choices: 
1: Public
2: Private
3: Void
4: Protected
5: Int
6: String
7: Package-Protected

Your answer: 
1: Public
2: Private
4: Protected
7: Package-Protected


Is incorrect, the correct answer is: 
1: Public
2: Private
4: Protected
7: Package-Protected
8
  • It might be cleaner to use hashmaps for lookup. Then you can just do a if(myHashMap.contains(key)) /.. do stuff .. / when iterating through their answers. Keeps the time linear instead of n^2, as well. You just take a slight hit on space complexity, so if that's a big concern you may need to work around that. But, it doesn't sound like it would be. Commented Jul 7, 2017 at 15:35
  • 1
    @svasa the header says he wants to avoid sorting. Commented Jul 7, 2017 at 15:39
  • 1
    are you sure you set matches to 1 before this block of code. If you set it to 0, the check should be matches == correctIndex.length-1 Commented Jul 7, 2017 at 15:41
  • @digidude I tried both ways and neither has worked for me. Commented Jul 7, 2017 at 15:44
  • depending on the data types used correctIndex[i] == response[j] may be wrong. It is possible you need correctIndex[i].equals(response[j]). Commented Jul 7, 2017 at 15:44

2 Answers 2

2

Why not use a Set? Two sets will be equal if they have the same elements regardless of the order!

An example:

Set<Integer> correctIndex = new HashSet<>();
Set<Integer> response = new HashSet<>();

correctIndex.add(1);
correctIndex.add(2);
correctIndex.add(3);

response.add(3);
response.add(2);
response.add(1);

// This will print true!
System.out.println(correctIndex.equals(response));
Sign up to request clarification or add additional context in comments.

3 Comments

Or use TreeSet and get the order for free.
A Set would omit duplicate answers which may be important.
@JacobG. nice observation!
0

Something like this should do it:

for(int i = 0;i<correctIndex.length;i++){
  int answer = correctIndex[i];
  boolean contained = false;
  for(int i = 0;i<response.length;i++){
    if(response[i]==answer){
      contained =true;
    }
  }
  if(contained = false){
    return false; // response is incorrect, add code to handle
  }
}
return true; // if it makes it through, response is correct

Comments

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.