Answer to Question #171247 in Java | JSP | JSF for Anushruti

Question #171247

Design a QUIZ Application in Java. The first is called the Warm-up Round; the second is the Challenge round. In the warm-up round, the user is asked a total of 5 simple questions. If they manage to answer at least 4 of them correctly to enter the next round. If the user is not capable of doing that, he is not permitted to proceed further.


1
Expert's answer
2021-03-12T14:54:48-0500
import java.util.Scanner;

public class Quiz {
    static Scanner in = new Scanner(System.in);

    public static int quiz(String[] questions, String[] answers) {
        int correctAnswers = 0;
        String answer;
        for (int i = 0; i < questions.length; i++) {
            System.out.println(i + 1 + ". " + questions[i]);
            answer = in.nextLine();
            if (answer.equalsIgnoreCase(answers[i])) {
                correctAnswers++;
            }
        }
        return correctAnswers;
    }

    public static void main(String[] args) {
        String[] warmupQuestions = {"What is the first color in the rainbow?",
                "Write the result expression: 2 + 2 * 2.",
                "What is the third planet from the sun?",
                "How many days are in a leap year.",
                "How many faces does a cube have?"};
        String[] warmupAnswers = {"Red", "6", "Earth", "366", "6"};

        String[] challengeQuestions = {"The tallest mountain in the world?",
                "The year of the first manned flight into space?",
                "What is 2 to the power of 10?",
                "Name a country that is also a continent.",
                "Name the chemical element that has the designation 'Hg'."};
        String[] challengeAnswers = {"Mount Everest",
                "1961",
                "1024",
                "Australia",
                "Mercury"};

        System.out.println("Warm-up Round:");
        if (quiz(warmupQuestions, warmupAnswers) >= 4) {
            System.out.println("\nChallenge Round:");
            System.out.println("Correct answers: " + quiz(challengeQuestions, challengeAnswers) + ".");
        } else {
            System.out.println("You have given less than four correct answers so do not advance to the next round.");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog