Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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