import java.util.
Scanner; // Import Scanner for user input
public class QuizGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create Scanner object
// Declare an array of quiz questions
String[] questions = {
"1. What is the capital of France?",
"2. Which planet is known as the Red Planet?",
"3. Who wrote 'Romeo and Juliet'?",
"4. What is the largest ocean on Earth?",
"5. What is the chemical symbol for water?"
};
// Declare a 2D array for the answer options corresponding to each question
String[][] options = {
{"A. Paris", "B. London", "C. Berlin", "D. Madrid"},
{"A. Earth", "B. Mars", "C. Venus", "D. Jupiter"},
{"A. William Blake", "B. Mark Twain", "C. William Shakespeare", "D. Charles Dickens
{"A. Atlantic", "B. Indian", "C. Arctic", "D. Pacific"},
{"A. H2O", "B. CO2", "C. O2", "D. H2"}
};
// Correct answers corresponding to each question
char[] answers = {'A', 'B', 'C', 'D', 'A'};
int score = 0; // Variable to store user's score
// Iterate over each question
for (int i = 0; i < questions.length; i++) {
System.out.println(questions[i]); // Print the question
for (String option : options[i]) {
System.out.println(option); // Print the options
}
// Prompt the user for input
System.out.print("Enter your answer (A, B, C, or D): ");
char userAnswer = Character.toUpperCase(scanner.next().charAt(0)); // Convert to up
// Use switch-case to handle input and compare with the correct answer
switch (userAnswer) {
case 'A':
case 'B':
case 'C':
case 'D':
if (userAnswer == answers[i]) {
score++; // Increment score if the answer is correct
}
break;
default:
System.out.println("Invalid input. Question skipped."); // Handle invalid i
}
System.out.println(); // Print a blank line for spacing
}
// Calculate the score percentage
double percentage = ((double) score / questions.length) * 100;
// Display the final score and percentage
System.out.println("You got " + score + " out of " + questions.length + " correct.");
System.out.println("Your score: " + percentage + "%");
scanner.close(); // Close the scanner
}
}