What is Quiz Game?
A quiz application is a software program designed to present a series of questions to participant on various topics or subjects. The participants interact with the application by providing answers to these questions. The application then evaluates the user’s responses, providing instant feedback on correctness and often tallying scores. The quiz applications vary in complexity, from simple text-based quizzes to more elaborate versions with multimedia elements, timers, and scoring mechanisms.
We are going to create basic quiz application, “Who Wants to Be a Millionaire?”. The show is an iconic quiz program known for its high-stakes format and captivating gameplay. Contestants tackle a series of increasingly challenging questions, aiming to win a million-dollar prize. Hosted by engaging personalities, the show’s dramatic tension builds as participants navigate through multiple-choice queries, using lifelines to aid their progress.
How to create Basic Quiz Application in Java?
- Program emulates a quiz show “Who Wants to Be a Millionaire?”
- We have created the three questions in quiz.
- Player will be greeted.
- We will present the three questions and corresponding multiple-choice options.
- Questions, options, and correct answers stored in arrays.
- Program iterates through questions.
- Display all questions one after another.
- Users input their answers via console input.
- Program compares user input with correct answers.
- Provide the immediate feedback like Correct or Incorrect Answer
- For correct answers, program increments the score;
- for incorrect, displays the right answer.
- Upon completing questions, program presents the final score.
- If all answers are correct.
- Congratulates user with a winning a million dollars message.
Program: Create Quiz Game application in Java
import java.util.Scanner; public class Millionaire { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = 0; String[] questions = { "Which country won the FIFA World Cup in 2018?", "Who painted the Mona Lisa?", "Which planet is known as the Red Planet?" }; String[] options = { "a) France\tb) Germany\tc) Brazil", "a) Michelangelo\tb) Leonardo da Vinci\tc) Pablo Picasso", "a) Mars\tb) Jupiter\tc) Saturn" }; String[] answers = {"a", "b", "a"}; System.out.println("Welcome to 'Who Wants to Be a Millionaire?'"); System.out.println("Answer the following questions:"); for (int i = 0; i < questions.length; i++) { System.out.println("\nQuestion " + (i + 1) + ": " + questions[i]); System.out.println("Options: " + options[i]); System.out.print("Your answer: "); String userAnswer = scanner.nextLine().toLowerCase(); if (userAnswer.equals(answers[i])) { System.out.println("Correct!"); score++; } else { System.out.println("Incorrect. The correct answer is: " + answers[i]); } } System.out.println("\nQuiz completed! Your final score: " + score + " out of " + questions.length); if (score == questions.length) { System.out.println("Congratulations! You've won a Million Dollars!"); } scanner.close(); } }
Output: Program to create quiz game in Java
Welcome to 'Who Wants to Be a Millionaire?' Answer the following questions: Question 1: Which country won the FIFA World Cup in 2018? Options: a) France b) Germany c) Brazil Your answer: a Correct! Question 2: Who painted the Mona Lisa? Options: a) Michelangelo b) Leonardo da Vinci c) Pablo Picasso Your answer: b Correct! Question 3: Which planet is known as the Red Planet? Options: a) Mars b) Jupiter c) Saturn Your answer: c Incorrect. The correct answer is: a Quiz completed! Your final score: 2 out of 3