Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Quiz Game: Trivia Challenge, Assignments of Programming Languages

This Java program simulates a simple quiz game, where the user is presented with questions and multiple-choice options. The program prompts the user to enter their answers and keeps track of the score. The questions cover various topics, including film, sports, music, and soccer. The program validates the user's input, provides feedback on correct and incorrect answers, and displays the final score as a percentage. This document could be useful for students learning Java programming, as it provides a practical example of implementing a basic quiz application using Java's scanner class and arrays. The program demonstrates key programming concepts such as loops, conditional statements, and string manipulation. By studying this document, students can gain insights into the design and implementation of a simple quiz game, which could be a valuable learning experience for those interested in developing interactive applications or improving their Java programming skills.

Typology: Assignments

2023/2024

Uploaded on 04/28/2024

muhaye-donat
muhaye-donat 🇺🇸

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROGRAMMING ASSIGNMENT 1
Code:
import java.util.Scanner;
public class Quiz {
public static void main(String[] args) {
// Initialize Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Define quiz questions and answers
String[] questions = {
"1. Who directed the film “Inception” (2010)?",
"2. What sport is associated with the term slam dunk”?",
"3. Which legendary musician is known as the “King of Pop”?",
"4. Who is the youngest player to ever score in a World Cup
tournament?",
"5. Which player holds the record for the most goals scored in
a single Premier League season?"
};
String[][] options = {
{"A. Quentin Tarantino", "B. Steven Spielberg", "C. Christopher
Nolan", "D. Martin Scorsese"},
{"A. Basketball", "B. Tennis", "C. Soccer", "D. Golf"},
{"A. Elvis Presley", "B. Freddie Mercury", "C. Bob Dylan", "D.
Michael Jackson"},
{"A. Lionel Messi", "B. Pelé", "C. Kylian Mbappé", "D. Diego
Maradona"},
{"A. Erling Haaland", "B. MCristiano Ronaldo", "C. Thierry
Henry", "D. Alan Shearer"}
};
char[] answers = {'C', 'A', 'D', 'B', 'A'}; // Correct answers for each
question
// Display instructions
System.out.println("Welcome to the Quiz Game!");
System.out.println("Please enter the letter corresponding to the
correct answer \n (A, B, C, or D).\n");
int score = 0; // Initialize score counter
// Iterate through each question
for (int i = 0; i < questions.length; i++) {
// Display question and options
System.out.println(questions[i]);
for (String option : options[i]) {
System.out.println(option);
}
pf3
pf4
pf5

Partial preview of the text

Download Quiz Game: Trivia Challenge and more Assignments Programming Languages in PDF only on Docsity!

PROGRAMMING ASSIGNMENT 1

Code:

import java.util.Scanner; public class Quiz { public static void main(String[] args) { // Initialize Scanner object for user input Scanner scanner = new Scanner(System.in); // Define quiz questions and answers String[] questions = { "1. Who directed the film “Inception” (2010)?", "2. What sport is associated with the term “slam dunk”?", "3. Which legendary musician is known as the “King of Pop”?", "4. Who is the youngest player to ever score in a World Cup tournament?", "5. Which player holds the record for the most goals scored in a single Premier League season?" }; String[][] options = { {"A. Quentin Tarantino", "B. Steven Spielberg", "C. Christopher Nolan", "D. Martin Scorsese"}, {"A. Basketball", "B. Tennis", "C. Soccer", "D. Golf"}, {"A. Elvis Presley", "B. Freddie Mercury", "C. Bob Dylan", "D. Michael Jackson"}, {"A. Lionel Messi", "B. Pelé", "C. Kylian Mbappé", "D. Diego Maradona"}, {"A. Erling Haaland", "B. MCristiano Ronaldo", "C. Thierry Henry", "D. Alan Shearer"} }; char[] answers = {'C', 'A', 'D', 'B', 'A'}; // Correct answers for each question // Display instructions System.out.println("Welcome to the Quiz Game!"); System.out.println("Please enter the letter corresponding to the correct answer \n (A, B, C, or D).\n"); int score = 0; // Initialize score counter // Iterate through each question for (int i = 0; i < questions.length; i++) { // Display question and options System.out.println(questions[i]); for (String option : options[i]) { System.out.println(option); }

// Prompt user for answer System.out.print("Choose your answer: "); char userAnswer = scanner.next().toUpperCase().charAt(0); // Convert input to uppercase and get first character // Validate user input if (userAnswer < 'A' || userAnswer > 'D') { System.out.println("Invalid input. Please enter A, B, C, or D."); i--; // Decrement i to repeat the same question continue; } // Compare user answer with correct answer if (userAnswer == answers[i]) { System.out.println("Correct!"); score++; // Increment score for correct answer } else { System.out.println("Incorrect!"); System.out.println("Correct Answer is " + answers[i]); } } // Calculate and display final score double percentage = (double) score / questions.length * 100; if (score > 0) { System.out.println("\nYou got " + score + " correct answers out of " + questions.length + " questions"); System.out.printf("Your Score: %.2f%%\n", percentage); } else { System.out.println("\nYou got " + score + " correct answer out of "

  • questions.length + " questions"); System.out.println("You failed!! Try Again"); } // Close scanner scanner.close(); } }

Explanation:

This Java program simulates a simple quiz game. The program prompt the user with a series of

questions and allow them to enter their answers.

Break down the program:

First, the line imports the Scanner class from the java.util package. Scanner is used to read input

from the user. Then I declared a class named Quiz where everything related to our quiz game

will be inside of it. The following main method is the entry point of this program. When it is

run, the code inside it will be executed.

Reference:

➔ Java User Input (Scanner). https://www.w3schools.com/java/java_user_input.asp.

w3schools.

➔ Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition.