Interactive Learning Quiz Java

👤 Sharing: AI
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class InteractiveQuiz {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        // Question and Answer data structure
        class Question {
            String questionText;
            List<String> options;
            int correctAnswerIndex;

            public Question(String questionText, List<String> options, int correctAnswerIndex) {
                this.questionText = questionText;
                this.options = options;
                this.correctAnswerIndex = correctAnswerIndex;
            }
        }

        // Create a list of questions
        List<Question> questions = new ArrayList<>();
        questions.add(new Question("What is the capital of France?",
                List.of("London", "Paris", "Berlin", "Rome"), 1));
        questions.add(new Question("What is 2 + 2?",
                List.of("3", "4", "5", "6"), 1));
        questions.add(new Question("Which planet is known as the Red Planet?",
                List.of("Earth", "Mars", "Jupiter", "Venus"), 1));
        questions.add(new Question("What is the largest mammal?",
                List.of("Elephant", "Blue Whale", "Giraffe", "Polar Bear"), 1));
        questions.add(new Question("What is the chemical symbol for water?",
                List.of("CO2", "O2", "H2O", "NaCl"), 2));

        int score = 0;
        int numQuestions = questions.size();

        // Present each question to the user
        for (int i = 0; i < numQuestions; i++) {
            int questionIndex = random.nextInt(questions.size()); // Randomize question order.
            Question question = questions.remove(questionIndex); //Remove to avoid duplicates if random generates the same number.

            System.out.println("\nQuestion " + (i + 1) + ": " + question.questionText);

            // Display options
            for (int j = 0; j < question.options.size(); j++) {
                System.out.println((j + 1) + ". " + question.options.get(j));
            }

            // Get user's answer
            int userAnswer;
            while (true) {
                System.out.print("Enter your answer (1-" + question.options.size() + "): ");
                try {
                    userAnswer = Integer.parseInt(scanner.nextLine());
                    if (userAnswer >= 1 && userAnswer <= question.options.size()) {
                        break; // Input is valid
                    } else {
                        System.out.println("Invalid input. Please enter a number between 1 and " + question.options.size());
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Invalid input. Please enter a number.");
                }
            }


            // Check if the answer is correct
            if (userAnswer - 1 == question.correctAnswerIndex) {
                System.out.println("Correct!");
                score++;
            } else {
                System.out.println("Incorrect. The correct answer was: " + question.options.get(question.correctAnswerIndex));
            }
        }

        // Display the final score
        System.out.println("\nQuiz complete!");
        System.out.println("Your score: " + score + " out of " + numQuestions);

        scanner.close();
    }
}
```
👁️ Viewed: 10

Comments