0% found this document useful (0 votes)
2 views17 pages

Java 1

The document contains a Java program for a quiz game that asks users a series of questions and evaluates their answers. It utilizes a Scanner for user input, validates the answers, and calculates the final score as a percentage. The program also includes explanations of its functionality and an example of expected output.

Uploaded by

rutkass2018
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)
2 views17 pages

Java 1

The document contains a Java program for a quiz game that asks users a series of questions and evaluates their answers. It utilizes a Scanner for user input, validates the answers, and calculates the final score as a percentage. The program also includes explanations of its functionality and an example of expected output.

Uploaded by

rutkass2018
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
You are on page 1/ 17

import java.util.

Scanner;

public class QuizGame {


public static void
main(String[] args) {
// Create a Scanner object
for user input
Scanner scanner = new
Scanner(System.in);

// Define questions,
options, and correct answers
String[] questions = {
"1. What is the capital
of France?\nA. Berlin\nB.
Madrid\nC. Paris\nD. Rome",
"2. Which planet is
known as the Red Planet?\nA.
Earth\nB. Mars\nC. Jupiter\nD.
Saturn",
"3. Who wrote 'Romeo
and Juliet'?\nA. Charles
Dickens\nB. Jane
Austen\nC. William
Shakespeare\nD. Mark Twain",
"4. What is the largest
ocean on Earth?\nA. Atlantic
Ocean\nB. Indian Ocean\nC.
Arctic Ocean\nD. Pacific
Ocean",
"5. What is the
chemical symbol for water?\
nA. H2O\nB. CO2\nC. O2\nD.
NaCl"
};

char[] correctAnswers =
{'C', 'B', 'C', 'D', 'A'};
int score = 0;

// Loop through each


question
for (int i = 0; i <
questions.length; i++) {

System.out.println(questions[i])
;
System.out.print("Enter
your
answer (A, B, C, or D): ");
char userAnswer =
scanner.next().toUpperCase().
charAt(0); // Read user input
and convert to uppercase

// Validate input
if (userAnswer < 'A' ||
userAnswer > 'D') {

System.out.println("Invalid
answer! Please enter A, B, C,
or D.");
i--; // Decrement i to
repeat the question
continue; // Skip to
the next iteration of the loop
}
// Compare user
answer with the correct answer
switch (userAnswer) {
case 'A':
case 'B':
case 'C':
case 'D':
if (userAnswer ==
correctAnswers[i]) {

System.out.println("Correct!");
score++; //
Increment score for correct
answer
} else {

System.out.println("Wrong! The
correct answer was " +
correctAnswers[i] + ".");
}
break;
}
}

// Calculate and display


final score
double percentageScore
= ((double) score /
questions.length) * 100;
System.out.printf("You
scored: %d out of %d (%.2f%
%)\n", score, questions.length,
percentageScore);

// Close the scanner


scanner.close();
}
}

Explanation of the Code:

1. Imports: We import the


Scanner class for user input.

2. Main Class: The QuizGame


class contains the main
method.
3. Variables: We define an
array of questions and their
corresponding correct
answers.

4. User Input: We use a


Scanner object to read user
input.

5. Loop: A loop iterates


through each question,
prompting the user for an
answer.

6. Input Validation: We check


if the user's answer is valid
(between A and D).
7. Switch Statement: We use
a switch statement to compare
the user's answer with the
correct answer and update the
score accordingly.

8. Final Score Calculation:


After all
questions are answered, we
calculate the percentage score
and display it.

9. Close Scanner: Finally, we


close the scanner to prevent
resource leaks.

Output Example:

When you run this program


and provide inputs like:
C
B
C
D
A
The output will be:
1. What is the capital of
France?
A. Berlin
B. Madrid
C. Paris
D. Rome
Enter your answer (A, B, C, or
D): C
Correct!
2. Which planet is known as
the Red Planet?
A. Earth
B. Mars
C. Jupiter
D. Saturn
Enter your answer (A, B, C, or
D): B
Correct!
3. Who wrote 'Romeo and
Juliet'?
A. Charles Dickens
B. Jane Austen
C. William Shakespeare
D. Mark Twain
Enter your answer (A, B, C, or
D): C
Correct!
4. What is the largest ocean on
Earth?
A. Atlantic Ocean
B. Indian Ocean
C. Arctic Ocean
D. Pacific Ocean
Enter your answer (A, B, C, or
D): D
Correct!
5. What is the chemical symbol
for water?

You might also like