Java Game

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

import java.util.

Scanner;

public class QuizGame {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int totalQuestions = 5;
int correctAnswers = 0;

// Display welcome message


System.out.println("Welcome to the Quiz Game!");
System.out.println("Answer the following questions:");

// Question 1
System.out.println("\n1. What is the capital of France?");
System.out.println("A. Paris");
System.out.println("B. London");
System.out.println("C. Rome");
System.out.println("D. Madrid");
char answer1 = scanner.next().charAt(0);
if (answer1 == 'A' || answer1 == 'a') {
correctAnswers++;
}

// Question 2
System.out.println("\n2. Which planet is known as the Red Planet?");
System.out.println("A. Venus");
System.out.println("B. Mars");
System.out.println("C. Jupiter");
System.out.println("D. Saturn");
char answer2 = scanner.next().charAt(0);
if (answer2 == 'B' || answer2 == 'b') {
correctAnswers++;
}

// Add more questions (3, 4, and 5) following the same pattern

// Calculate the score


double score = (double) correctAnswers / totalQuestions * 100;

// Display the final score


System.out.println("\nQuiz completed!");
System.out.println("Your score: " + score + "%");
// Close the scanner
scanner.close();
}
}

You might also like