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.
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.");
}
}
}
Comments
Leave a comment