0% found this document useful (0 votes)
3 views

Import Java

The document contains a Java program for a quiz game that asks five questions related to general knowledge and Java programming. It allows users to input their answers, validates the input, and keeps track of the score. At the end of the quiz, it calculates and displays the user's score and percentage.

Uploaded by

omernawabi3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Import Java

The document contains a Java program for a quiz game that asks five questions related to general knowledge and Java programming. It allows users to input their answers, validates the input, and keeps track of the score. At the end of the quiz, it calculates and displays the user's score and percentage.

Uploaded by

omernawabi3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

public class QuizGame {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int score = 0

// Array of questions

String[] questions = {

"What is the capital of France?",

"Which data type is used to store decimal numbers in Java?",

"Who developed the Java programming language?",

"Which keyword is used to define a constant in Java?",

"What is the output of 5 + 3 * 2 in Java?"

// Array of options

String[][] options = {

{"A. Berlin", "B. Madrid", "C. Paris", "D. Rome"},

{"A. int", "B. double", "C. float", "D. boolean"},

{"A. James Gosling", "B. Dennis Ritchie", "C. Bjarne Stroustrup", "D. Guido van Rossum"},

{"A. final", "B. constant", "C. static", "D. define"},

{"A. 16", "B. 13", "C. 10", "D. 11"}

};

// Array of correct answers

char[] correctAnswers = {'C', 'B', 'A', 'A', 'B'};

// Loop through questions

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

System.out.println("\n" + (i + 1) + ". " + questions[i]);

for (String option : options[i]) {

System.out.println(option);

}
System.out.print("Enter your answer (A, B, C, or D): ");

char answer = Character.toUpperCase(scanner.next().charAt(0));

// Validate input

if (answer < 'A' || answer > 'D') {

System.out.println("Invalid input! Please enter A, B, C, or D.");

i--; // Retry the question

continue;

// Check answer

if (answer == correctAnswers[i]) {

System.out.println("Correct!");

score++;

} else {

System.out.println("Incorrect. The correct answer was " + correctAnswers[i] + ".");

// Calculate and display final score

double percentage = ((double) score / questions.length) * 100;

System.out.println("\nYou scored " + score + " out of " + questions.length + " (" + percentage +
"%).");

scanner.close();

You might also like