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

Java QuizGame Assignment

The document contains a Java program for a quiz game that asks users five questions with multiple-choice answers. It tracks the user's score based on their responses and calculates the percentage of correct answers at the end. The program uses a Scanner for input and includes error handling for invalid inputs.

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 Assignment

The document contains a Java program for a quiz game that asks users five questions with multiple-choice answers. It tracks the user's score based on their responses and calculates the percentage of correct answers at the end. The program uses a Scanner for input and includes error handling for invalid inputs.

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;

public class QuizGame {


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

// Questions, options, and correct 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. 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"}
};

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


int score = 0;

// Loop through questions


for (int i = 0; i < questions.length; i++) {
System.out.println(questions[i]);
for (String option : options[i]) {
System.out.println(option);
}

System.out.print("Enter your answer (A, B, C, or D): ");


char userAnswer = Character.toUpperCase(scanner.next().charAt(0));

// Compare input using if and switch


switch (userAnswer) {
case 'A':
case 'B':
case 'C':
case 'D':
if (userAnswer == answers[i]) {
score++;
}
break;
default:
System.out.println("Invalid input. Question skipped.");
}
System.out.println();
}

// Calculate and display percentage


double percentage = ((double) score / questions.length) * 100;
System.out.println("You got " + score + " out of " + questions.length + " correct.");
System.out.println("Your score: " + percentage + "%");

scanner.close();
}
}

You might also like