I am currently working on a schoolwork assignment to create a quiz demo in java. However, when I shuffle the question answer options, the correct answer index is mixed up and I cannot fix it. Please help. Thank you
Shuffle method: private String[] shuffleOptions(String[] options, int answer) { Random ran = new Random();
// Answer value before shuffling
String answerValue = options[answer];
// Shuffle options randomly
for (int i = options.length - 1; i > 0; i--) {
int index = ran.nextInt(i + 1);
String temp = options[index];
options[index] = options[i];
options[i] = temp;
}
// Find the new correct answer index after shuffling
for (int i = 0; i < options.length; i++) {
if (options[i].equals(answerValue)) {
answerTemp = i;
}
}
return options;
}
Use:
import java.util.Random;
import java.util.Scanner;
public class Quiz {
private String name;
private Question[] questions;
private int capacity;
private int size;
private int score;
private int answerTemp;
Quiz(){
this.name = "Quiz" + String.valueOf(Math.random()*1000);
this.questions = new Question[5];
this.capacity = 5;
this.size = 0;
this.score = 0;
this.answerTemp = 0;
}
private void increaseCapacity() { // Creates new quiz with double capacity/length for questions
capacity *= 2;
Question[] doubleQuestions = new Question[capacity];
if (size > 0) {
System.arraycopy(questions, 0, doubleQuestions, 0, size);
}
questions = doubleQuestions;
}
public void addQuestion(Question question) {
if (size >= capacity) { // Reaches max capacity/length of questions
increaseCapacity();
}
questions[size++] = question;
}
private String[] shuffleOptions(String[] options, int answer) {
Random ran = new Random();
// Answer value before shuffling
String answerValue = options[answer];
// Shuffle options randomly
for (int i = options.length - 1; i > 0; i--) {
int index = ran.nextInt(i + 1);
String temp = options[index];
options[index] = options[i];
options[i] = temp;
}
// Find the new correct answer index after shuffling
for (int i = 0; i < options.length; i++) {
if (options[i].equals(answerValue)) {
answerTemp = i;
}
}
return options;
}
private String finalScore() { //Returns (percentage) final score
int finalScore = (int) ((double) score / size * 100);
return("FINAL SCORE: " + finalScore + "%" + "!");
}
public void giveQuiz() {
Scanner userInput = new Scanner(System.in);
int userAnswer = 0;
for (int i = 0; i < size; i++) {
Question currentQuestion = questions[i];
System.out.println("Question " + (i + 1) + ") : " + currentQuestion.getQuestion());
String[] shuffledOptions = shuffleOptions(currentQuestion.getOptions(), currentQuestion.getAnswer());
currentQuestion.setAnswer(answerTemp); // Sets correct answer value to index
for (int x = 0; x < shuffledOptions.length; x++) { // Prints shuffled options
System.out.println("(" + (x + 1) + ") " + shuffledOptions[x]);
}
System.out.println("\n Type Answer (1-4): ");
userAnswer = userInput.nextInt();
if (userAnswer == currentQuestion.getAnswer()) { //Checks for correct answer
System.out.println("\nCORRECT ANSWER");
score++;
}
else {
System.out.println("\nWRONG ANSWER");
}
}
System.out.println(finalScore());
userInput.close();
} //end of giveQuiz
}
Input number for correct answer Outputs incorrect
Your user input starts with 1, but your answer starts with 0 This is your user input
This is the answer generate process
If the first answer is correct, then the
answerTempis 0 instead of 1, that's the problem