0% found this document useful (0 votes)
1 views2 pages

Java QuizGame Explained

The document is a Java program for a quiz game that asks users five questions with multiple-choice answers. It tracks the user's score based on correct answers and displays the final score and percentage at the end. The program handles user input and invalid responses appropriately.

Uploaded by

Awabilla Philip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Java QuizGame Explained

The document is a Java program for a quiz game that asks users five questions with multiple-choice answers. It tracks the user's score based on correct answers and displays the final score and percentage at the end. The program handles user input and invalid responses appropriately.

Uploaded by

Awabilla Philip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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


}
}

You might also like