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

Java Quiz Game Code Example

The document contains a Java program for a quiz game that asks users five multiple-choice questions. It includes questions, answer options, and validates user input while tracking the score. At the end, it calculates and displays the user's score as a percentage of correct answers.

Uploaded by

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

Java Quiz Game Code Example

The document contains a Java program for a quiz game that asks users five multiple-choice questions. It includes questions, answer options, and validates user input while tracking the score. At the end, it calculates and displays the user's score as a percentage of correct answers.

Uploaded by

اللورد
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

import [Link].

Scanner; // Import Scanner class to read user input

public class QuizGame {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

// Initialize questions, options, and answers


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?"
};

String[][] options = {
{"A. London", "B. Berlin", "C. Paris", "D. Madrid"},
{"A. Venus", "B. Mars", "C. Jupiter", "D. Saturn"},
{"A. Charles Dickens", "B. William Shakespeare", "C. Mark Twain", "D. Jane Austen"},
{"A. Atlantic Ocean", "B. Indian Ocean", "C. Arctic Ocean", "D. Pacific Ocean"},
{"A. O2", "B. H2O", "C. CO2", "D. NaCl"}
};

char[] answers = {'C', 'B', 'B', 'D', 'B'}; // Correct answers

int score = 0; // To track number of correct answers

// Loop through each question


for (int i = 0; i < [Link]; i++) {
[Link](questions[i]);
for (String option : options[i]) {
[Link](option);
}

[Link]("Enter your answer (A, B, C, D): ");


String input = [Link]().trim().toUpperCase();

// Input validation
while (![Link]("A") && ![Link]("B") && ![Link]("C") && !
[Link]("D")) {
[Link]("Invalid input. Please enter A, B, C, or D: ");
input = [Link]().trim().toUpperCase();
}

// Compare user's answer with the correct answer


switch (input) {
case "A":
if (answers[i] == 'A') score++;
break;
case "B":
if (answers[i] == 'B') score++;
break;
case "C":
if (answers[i] == 'C') score++;
break;
case "D":
if (answers[i] == 'D') score++;
break;
}

[Link](); // Print a blank line for readability


}

// Calculate and display final score


double percentage = ((double) score / [Link]) * 100;
[Link]("Quiz Completed!");
[Link]("You got " + score + " out of " + [Link] + " correct.");
[Link]("Your final score: %.2f%%\n", percentage);

[Link]();
}
}

Screenshot for output

You might also like