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

The Java Quiz Game

The document describes a Java quiz game program that asks multiple choice questions, tracks the user's score, and displays the final percentage. The program initializes questions and answers, gets user input, checks the responses, and calculates the score at the end.

Uploaded by

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

The Java Quiz Game

The document describes a Java quiz game program that asks multiple choice questions, tracks the user's score, and displays the final percentage. The program initializes questions and answers, gets user input, checks the responses, and calculates the score at the end.

Uploaded by

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

The Java Quiz Game

Code:

import java.util.Scanner;

public class JavaQuizGame {

public static void main(String[] args) {


// Initialize a Scanner for user input
Scanner scanner = new Scanner(System.in);

// Define the quiz questions and correct answers


String[] questions = {
"What is Uopeople university Located?",
"Who is the instructor for programming 1102?",
"How many units are there in CS-1102 course?",
"Who is supposed to make sure assignments are updated?",
};

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

// Initialize variables to track user score


int totalQuestions = questions.length;
int correctResponses = 0;

// Display the quiz questions and get user input


for (int i = 0; i < totalQuestions; i++) {
System.out.println("Question " + (i + 1) + ":\n " + questions[i]);
System.out.println("\nA) Nine units");
System.out.println("B) Dr. Ursa Sayeed");
System.out.println("C) Pasadena , California");
System.out.println("D) The Student \n");
System.out.print("Your answer (A, B, C, or D): ");

// Get user input


char userAnswer = scanner.next().toUpperCase().charAt(0);

// Compare user's answer with the correct answer


if (userAnswer == correctAnswers[i]) {
System.out.println("Correct!\n");
correctResponses++;
} else {
System.out.println("Incorrect. The correct answer is " + correctAnswers[i] + "\n");
}
}

// Calculate and display the final score


double scorePercentage = (double) correctResponses / totalQuestions * 100;
System.out.println("Quiz completed! Your final score: " + scorePercentage + "%");

// Close the scanner


scanner.close();
}
}
SCREENSHOT:

You might also like