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

Java Quiz Game Program Code

The Java Quiz Game program allows users to answer five multiple-choice questions and calculates their score based on correct answers. It utilizes the Scanner class for input and includes validation for user responses. The final score is presented as a percentage of correct answers out of the total questions asked.

Uploaded by

doncmwe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Java Quiz Game Program Code

The Java Quiz Game program allows users to answer five multiple-choice questions and calculates their score based on correct answers. It utilizes the Scanner class for input and includes validation for user responses. The final score is presented as a percentage of correct answers out of the total questions asked.

Uploaded by

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

Java Quiz Game Assignment

Program Code

import [Link];

public class QuizGame {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);


int correctAnswers = 0;
String userAnswer;

[Link](" WELCOME TO THE JAVA QUIZ GAME ");


[Link]("Type A, B, C, or D to answer each question.\n");

[Link]("1. Which data type is used to store whole numbers in Java?");


[Link]("A. float");
[Link]("B. int");
[Link]("C. boolean");
[Link]("D. char");
[Link]("Your answer: ");
userAnswer = [Link]().toUpperCase();

if ([Link]("[ABCD]")) {
switch (userAnswer) {
case "B":
correctAnswers++;
break;
}
} else {
[Link]("Invalid answer! Marked as wrong.");
}

// (Other questions omitted here for brevity, same structure)

double percentage = (correctAnswers / 5.0) * 100;

[Link](" QUIZ COMPLETE ");


[Link]("Correct Answers: " + correctAnswers + " out of 5");
[Link]("Your Score: %.2f%%\n", percentage);

[Link]();
}
}

Explanation of the Program

This Java program simulates a simple quiz game consisting of five multiple-choice
questions. The Scanner class is used to collect input from the user. For each question, the
program displays four answer choices labeled A, B, C, and D. The user inputs their
answer, and an if-statement validates the input to ensure it matches acceptable options. A
switch-case statement is then used to compare the user’s input with the correct answer
and update the score accordingly.

After all five questions have been answered, the program computes the final score by
dividing the number of correct answers by the total number of questions. The result is
then multiplied by 100 to produce a percentage score. Finally, the program displays the
number of correct answers and the final score.

This program follows Java best practices, such as meaningful variable names, structured
logic, input validation, and clear output formatting.

Screenshots

You might also like