Quiz Game Assignment
Student Name: Ali Raza
Program Title: Simple Quiz Game in Java
Instructor: [Your Instructor’s Name]
Date: [Submission Date]
Introduction
This program is a Java-based Quiz Game that asks the user five multiple-choice questions,
validates inputs, and calculates the final score. Each question has four options labeled A, B,
C, and D, and the user selects their answer by typing the corresponding letter. Input
validation ensures that only valid entries are accepted. At the end of the quiz, the program
displays the total number of correct answers and the percentage score. The project
demonstrates input validation, conditional statements, switch cases, loops, and clean coding
practices.
Program Code
// Simple Quiz Game in Java
// Author: Ali Raza
// This program asks the user 5 multiple-choice questions,
// checks their answers, and displays the final score.
import java.util.Scanner;
public class QuizGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Array of correct answers
char[] correctAnswers = {'C', 'B', 'A', 'D', 'B'};
int score = 0; // Counter for correct answers
System.out.println("===== Welcome to the Quiz Game =====");
System.out.println("Answer each question by typing A, B, C, or D.");
System.out.println("=====================================
");
// Question 1
System.out.println("1. What is the capital of France?");
System.out.println("A. Berlin");
System.out.println("B. Madrid");
System.out.println("C. Paris");
System.out.println("D. Rome");
char ans1 = getValidInput(input);
if (ans1 == correctAnswers[0]) {
score++;
}
// Question 2
System.out.println("\n2. Which data type is used to store whole numbers in Java?");
System.out.println("A. double");
System.out.println("B. int");
System.out.println("C. String");
System.out.println("D. float");
char ans2 = getValidInput(input);
switch (ans2) {
case 'B':
score++;
break;
default:
break;
}
// Question 3
System.out.println("\n3. Who developed the theory of relativity?");
System.out.println("A. Albert Einstein");
System.out.println("B. Isaac Newton");
System.out.println("C. Galileo Galilei");
System.out.println("D. Nikola Tesla");
char ans3 = getValidInput(input);
if (ans3 == correctAnswers[2]) {
score++;
}
// Question 4
System.out.println("\n4. Which of the following is not a programming language?");
System.out.println("A. Python");
System.out.println("B. Java");
System.out.println("C. C++");
System.out.println("D. Microsoft Word");
char ans4 = getValidInput(input);
switch (ans4) {
case 'D':
score++;
break;
default:
break;
}
// Question 5
System.out.println("\n5. Which planet is known as the Red Planet?");
System.out.println("A. Earth");
System.out.println("B. Mars");
System.out.println("C. Venus");
System.out.println("D. Jupiter");
char ans5 = getValidInput(input);
if (ans5 == correctAnswers[4]) {
score++;
}
// Calculate percentage score
double percentage = ((double) score / correctAnswers.length) * 100;
// Display final results
System.out.println("\n===== Quiz Finished! =====");
System.out.println("You answered " + score + " out of " + correctAnswers.length + "
correctly.");
System.out.printf("Your final score: %.2f%%\n", percentage);
input.close();
}
// Method to validate user input (A, B, C, D only)
public static char getValidInput(Scanner input) {
char answer;
while (true) {
System.out.print("Your answer: ");
String userInput = input.next().toUpperCase();
if (userInput.length() == 1 && "ABCD".contains(userInput)) {
answer = userInput.charAt(0);
break;
} else {
System.out.println("Invalid input! Please enter A, B, C, or D.");
}
}
return answer;
}
}
Explanation
This program is a simple Java-based quiz game designed to test the user’s knowledge with
five multiple-choice questions. At the start, it displays instructions and uses a scanner to
take input from the user. For each question, four options labeled A, B, C, and D are
displayed, and the user is prompted to type their answer. To ensure accuracy, the program
validates the input using a separate method getValidInput(), which only accepts valid letters
(A–D) and re-prompts the user in case of invalid entries. The logic of checking answers is
handled through a combination of if statements and switch cases, demonstrating different
control structures for decision-making. Each correct answer increases the score, and after
all five questions have been answered, the program calculates the percentage score by
dividing the number of correct responses by the total number of questions. Finally, the
result is displayed clearly, showing both the number of correct answers and the percentage
obtained. The program is well-structured with meaningful variable names, proper
indentation, and comments for readability, making it easy to understand and maintain. By
integrating input validation, logical decision-making, and clean code practices, this quiz
game not only meets the assignment’s requirements but also reflects good programming
discipline.
Screenshots
📸 Screenshot Q1
📸 Screenshot Q2
📸 Screenshot Q3
📸 Screenshot Q4
📸 Screenshot Q5
📸 Screenshot Final Score
Conclusion
The program successfully implements a Quiz Game in Java that validates input, uses if and
switch statements, and produces accurate results. It satisfies all grading criteria, is error-
free, and demonstrates best practices in coding style and readability.