Reputation: 37
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
Upvotes: 1
Views: 340
Reputation: 683
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));
Upvotes: 2
Reputation: 1094
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
Upvotes: 0