I have a simple quiz app in Spring Boot, that should show random questions (20 questions in total.) But the app instead stops at a random number of questions, sometimes 4/20, sometimes 15/20, etc. Not sure where the mistake is. Here's my code:
public class Quiz implements ActionListener {
List<Integer> questionIndexes = new ArrayList<>();
String[] questions = {
// 20 different questions
};
String[][] options = {
// Four options per question
};
char[] answers = {
// Answers
};
char answer;
int index;
int correctGuesses = 0;
int totalQuestions = questions.length;
int result;
int seconds = 10;
Timer timer = new Timer(1000, e -> {
seconds--;
secondsLeft.setText(String.valueOf(seconds));
if (seconds <= 0) {
displayAnswer();
}
});
public Quiz() {
for (int i = 0; i < totalQuestions; i++) {
questionIndexes.add(i);
}
Random random = new Random();
index = random.nextInt(totalQuestions);
answer = answers[index];
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// and the rest of the code for UI
nextQuestion();
}
public void nextQuestion() {
if (index >= totalQuestions) {
results();
} else {
index++;
textField.setText("Question " + (index + 1));
textArea.setText(questions[index]);
answerLabelA.setText(options[index][0]);
answerLabelB.setText(options[index][1]);
answerLabelC.setText(options[index][2]);
answerLabelD.setText(options[index][3]);
timer.start();
}
}
@Override
public void actionPerformed(ActionEvent e) {
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
if(e.getSource() == buttonA) {
answer = 'A';
if(answer == answers[index]) {
correctGuesses++;
}
}
if(e.getSource() == buttonB) {
answer = 'B';
if(answer == answers[index]) {
correctGuesses++;
}
}
if(e.getSource() == buttonC) {
answer = 'C';
if(answer == answers[index]) {
correctGuesses++;
}
}
if(e.getSource() == buttonD) {
answer = 'D';
if(answer == answers[index]) {
correctGuesses++;
}
}
displayAnswer();
}
public void displayAnswer() {
timer.stop();
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
if(answers[index] != 'A') {
answerLabelA.setForeground(new Color(255,0,0));
}
if(answers[index] != 'B') {
answerLabelB.setForeground(new Color(255,0,0));
}
if(answers[index] != 'C') {
answerLabelC.setForeground(new Color(255,0,0));
}
if(answers[index] != 'D') {
answerLabelD.setForeground(new Color(255,0,0));
}
Timer pause = new Timer(2000, e -> {
answerLabelA.setForeground(new Color(210,210,210));
answerLabelB.setForeground(new Color(210,210,210));
answerLabelC.setForeground(new Color(210,210,210));
answerLabelD.setForeground(new Color(210,210,210));
answer = ' ';
seconds = 10;
secondsLeft.setText(String.valueOf(seconds));
buttonA.setEnabled(true);
buttonB.setEnabled(true);
buttonC.setEnabled(true);
buttonD.setEnabled(true);
questionIndexes.remove(Integer.valueOf(index));
index++;
nextQuestion();
});
pause.setRepeats(false);
pause.start();
}
public void results() {
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
result = (int)((correctGuesses/(double)totalQuestions) * 100);
textField.setText("Your score: ");
textArea.setText("");
answerLabelA.setText("");
answerLabelB.setText("");
answerLabelC.setText("");
answerLabelD.setText("");
numberRight.setText("(" + correctGuesses + "/" + totalQuestions + ")");
percentage.setText(result + "%");
jFrame.add(numberRight);
jFrame.add(percentage);
}
}
And every time I run the code, it randomly shows 5, 18, or 9 questions, and then the app stops, instead of running through all 20 questions randomly.
Aucun commentaire:
Enregistrer un commentaire