Java Program
Explanation
This program mimics a simple multiple-choice quiz game to be played by students,
with five questions and four A, B, C, D options for each question. The user enters their
answers, the program determines correctness, interprets the score, and calculates the percent
score as well.
The program employs the Scanner class for inputting user supplied information.
The program uses if statements to validate and compare answers.
The program uses switch statement for the multiple-choice input.
At the end, the score will be displayed as a percentage score.
This is consistent and builds on fundamental computing concepts learned in
introductory Java programming classes (Eck, 2022).
Java Program Code
// Import Scanner for user input
import java.util.Scanner;
public class QuizGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Variable to keep track of correct answers
int score = 0;
int totalQuestions = 5;
System.out.println("Welcome to the Quiz Game!");
System.out.println("Please answer each question by typing A, B, C, or D.\n");
// 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");
System.out.print("Your answer: ");
String answer1 = scanner.next().toUpperCase();
switch (answer1) {
case "C":
score++;
break;
default:
// no points added
break;
}
// Question 2
System.out.println("\n2. Which language is used for Android app development?");
System.out.println("A. Python");
System.out.println("B. Java");
System.out.println("C. C++");
System.out.println("D. Swift");
System.out.print("Your answer: ");
String answer2 = scanner.next().toUpperCase();
if (answer2.equals("B")) {
score++;
}
// Question 3
System.out.println("\n3. Who developed the theory of relativity?");
System.out.println("A. Isaac Newton");
System.out.println("B. Galileo Galilei");
System.out.println("C. Albert Einstein");
System.out.println("D. Nikola Tesla");
System.out.print("Your answer: ");
String answer3 = scanner.next().toUpperCase();
switch (answer3) {
case "C":
score++;
break;
default:
break;
}
// Question 4
System.out.println("\n4. Which planet is known as the Red Planet?");
System.out.println("A. Earth");
System.out.println("B. Jupiter");
System.out.println("C. Venus");
System.out.println("D. Mars");
System.out.print("Your answer: ");
String answer4 = scanner.next().toUpperCase();
if (answer4.equals("D")) {
score++;
}
// Question 5
System.out.println("\n5. Which of the following is a programming language?");
System.out.println("A. HTML");
System.out.println("B. CSS");
System.out.println("C. Java");
System.out.println("D. SQL");
System.out.print("Your answer: ");
String answer5 = scanner.next().toUpperCase();
switch (answer5) {
case "C":
score++;
break;
default:
break;
}
// Calculate final score percentage
double percentage = ((double) score / totalQuestions) * 100;
// Display result
System.out.println("\nQuiz Completed!");
System.out.println("You answered " + score + " out of " + totalQuestions + "
correctly.");
System.out.printf("Your final score: %.2f%%\n", percentage);
scanner.close();
}
}
References
Eck, D. J. (2022). Introduction to programming using Java, version 9, JavaFX edition. Hobart
and William Smith Colleges. Licensed under CC BY 4.0. https://math.hws.edu/javanotes9/
Liang, Y. D. (2022). Introduction to Java programming and data structures, comprehensive
version (12th ed.). Pearson.