0% found this document useful (0 votes)
7 views3 pages

Java Quiz Game Assignment

The document describes a Java program that simulates a multiple-choice quiz game with five questions and four answer options. It tracks the user's correct responses and calculates the final score as a percentage. The program uses 'if' and 'switch' statements for input validation and includes comments for clarity.

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)
7 views3 pages

Java Quiz Game Assignment

The document describes a Java program that simulates a multiple-choice quiz game with five questions and four answer options. It tracks the user's correct responses and calculates the final score as a percentage. The program uses 'if' and 'switch' statements for input validation and includes comments for clarity.

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/ 3

Java Quiz Game Assignment

Student Name: [Your Name]

Course: [Course Name]

Date: [Submission Date]

This Java program is designed to simulate a simple multiple-choice quiz game.

It asks the user five questions, each with four answer options (A, B, C, and D).

The user is prompted to type the letter corresponding to their chosen answer.

After each input, the program compares the user's answer with the correct answer

and keeps track of the number of correct responses. It then calculates the final

score as a percentage of correct answers out of the total questions.

The logic uses both 'if' and 'switch' statements to validate and process user

input, as required. Comments are included throughout the code to explain each

section clearly.

Below is the complete program with explanations embedded as comments:

import java.util.Scanner;

public class QuizGame {


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

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

// Options for 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
char[] answers = {'A', 'B', 'C', 'D', 'A'};
int score = 0; // Track number of correct answers

// Loop through all 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));

// Validate and compare answers using switch and if


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 score 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