Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What would be the best way to loop the question if I get the answer wrong? For example, if I were to answer question 1

What would be the best way to loop the question if I get the answer wrong? For example, if I were to answer question 1 as B instead of A, how would I output "Incorrect, try again." and make the question repeat until it's answered correctly.

import java.util.Scanner;

public class AHHH {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

// Welcome the user

System.out.println("Welcome to my quiz program!");

// Prompt the user for their name and store it in a variable

System.out.print("Please enter your name: ");

String name = scan.nextLine();

// Thank the user by name and tell them to get ready to take the quiz

System.out.println("Thank you, " + name + ". Get ready to take the quiz!");

String[] questions = {

"What is the capital of France?",

"What is the largest planet in our solar system?",

"Who painted the Mona Lisa?",

"What is the currency of Spain?",

"What is the highest mountain peak in the world?"

};

// Possible answers for each question

String[][] answers = {

{"A) Paris", "B) Marseille", "C) Lyon", "D) Nice"},

{"A) Earth", "B) Mars", "C) Jupiter", "D) Saturn"},

{"A) Leonardo da Vinci", "B) Michelangelo", "C) Raphael", "D) Titian"},

{"A) Euro", "B) Peso", "C) Dollar", "D) Franc"},

{"A) Mount Everest", "B) K2", "C) Lhotse", "D) Makalu"}

};

int numCorrectAnswers = 0;

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

// Loop through each question

String userAnswer="";

boolean validAnswer = false;

for (int i = 0; i < questions.length; i++) {

// Display the question and possible answers

System.out.println(questions[i]);

for (int j = 0; j < answers[i].length; j++) {

System.out.println(answers[i][j]);

}

// Loop until the user enters a valid answer

do {

System.out.print("Your answer: ");

userAnswer = scan.nextLine().toUpperCase();

// Validate the user's answer with a switch statement

switch (userAnswer) {

case "A":

case "B":

case "C":

case "D":

validAnswer = true;

break;

default:

System.out.println("Invalid response. Please enter a letter between A and D.");

break;

}

} while (!validAnswer);

// Check if the user's answer is correct

if (userAnswer.charAt(0) == correctAnswers[i]) {

numCorrectAnswers++;

}

}

// Output the number of correct answers

System.out.println("You got " + numCorrectAnswers + " out of " + questions.length +" questions correct.");

// Output a statement based on the score

if (numCorrectAnswers == 0) {

System.out.println("Well, that wasn't good.");

} else if (numCorrectAnswers == 1) {

System.out.println("You can do better than that.");

} else if (numCorrectAnswers == 2) {

System.out.println("Not bad, but there's room for improvement.");

} else if (numCorrectAnswers == 3) {

System.out.println("Nice job, keep it up!");

} else if (numCorrectAnswers == 4) {

System.out.println("Great work! So close to a perfect score.");

} else {

System.out.println("Congratulations! You got a perfect score.");

}

// Output the questions that the user got wrong and the correct answers

if (numCorrectAnswers < questions.length) {

System.out.println("Here are the questions that you got wrong and the correct answers:");

for (int i = 0; i < questions.length; i++) {

if (userAnswer.charAt(0) != correctAnswers[i]) {

System.out.println("Question " + (i + 1) + ": " + questions[i]);

System.out.println("Your answer: " + userAnswer);

System.out.println("Correct answer: " + correctAnswers[i]);

System.out.println("Fun fact: ");

if (i == 0) {

System.out.println("Paris is the capital of France and is known for its iconic landmarks such as the Eiffel Tower and the Louvre Museum.");

} else if (i == 1) {

System.out.println("Jupiter is the largest planet in our solar system and is known for its Great Red Spot, a giant storm larger than the size of Earth.");

} else if (i == 2) {

System.out.println("The Mona Lisa is a painting by the famous artist Leonardo da Vinci and is considered one of the most iconic artworks in the world.");

} else if (i == 3) {

System.out.println("The Euro is the currency of Spain and is used by 19 of the 27 European Union member states.");

} else if (i == 4) {

System.out.println("Mount Everest is the highest mountain peak in the world, with a summit elevation of 8,848 meters (29,029 ft).");

}

}

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions