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

Programming 1 Unit 1

Uploaded by

Malou Majok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Programming 1 Unit 1

Uploaded by

Malou Majok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Here is how to create a Java program to simulate a quiz game.

This code prompts the user with


questions and then computes and displays the final score based on their answers.

import java.util.Scanner;

public class QuizGame {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int score = 0;

// Question 1

System.out.println("Question 1: What is the capital of USA?");

System.out.println("A) Madrid");

System.out.println("B) Berlin");

System.out.println("C) Washington DC");

System.out.println("D) Rome");

String answer = scanner.next();

if (answer.equalsIgnoreCase("C) Washington DC")) {

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

score++;

} else {

System.out.println("Incorrect. The correct answer is C) Paris.");

// Question 2
System.out.println("Question 2: Which of the following is not an animal?");

System.out.println("A) Dog");

System.out.println("B) Cat");

System.out.println("C) goat");

System.out.println("D) hen");

answer = scanner.next();

if (answer.equalsIgnoreCase("D) hen")) {

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

score++;

} else {

System.out.println("Incorrect. The correct answer is D) hen.");

// Question 3

System.out.println("Question 3: Who is the first president of United State of America?");

System.out.println("A) John Adam");

System.out.println("B) Thomas Jefferson");

System.out.println("C) James Madison");

System.out.println("D) George washington");

answer = scanner.next();

if (answer.equalsIgnoreCase("D) George Washington")) {

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

score++;
} else {

System.out.println("Incorrect. The correct answer is D) George Washington.");

// Question 4

System.out.println("Question 4: Which of the following is not a planet in our solar system?");

System.out.println("A) Mercury");

System.out.println("B) Venus");

System.out.println("C) Earth");

System.out.println("D) Mars");

answer = scanner.next();

if (answer.equalsIgnoreCase("C) Earth")) {

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

score++;

} else {

System.out.println("Incorrect. The correct answer is C) Earth.");

// Question 5

System.out.println("Question 5: Which of the following is odd?");

System.out.println("A) plastics");

System.out.println("B) Mental Cans");

System.out.println("C) Vacuum Pouches");

System.out.println("D) Vegetables");
answer = scanner.next();

if (answer.equalsIgnoreCase("D) Vegetables")) {

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

score++;

} else {

System.out.println("Incorrect. The correct answer is D) Vegetables.");

System.out.println("Your final score is: " + score + "/" + 5);

This program prompts the user with five multiple-choice questions and allows them to enter their
answers. After the user has entered their answer for each question, the program compares it to the
correct answer and keeps track of the score. Finally, the program displays the final score based on the
number of correct answers.

You might also like