Create a simple 5-question quiz with three choices that will display the score of the user. the quiz should be about your favorite game/series. Make use of control structures, arrays and functions in your Java program. Make sure the Interactive option is enabled to allow user input
import java.util.Random;
import java.util.Scanner;
public class Main {
public static String[] questions = {"When was the first International?",
"Who is the winner of the third International?",
"Which hero has the ability 'Cold feet'?",
"How much health and mana points restores the Cheese?",
"Which of these heroes was not in the DOTA 1?"};
public static String[][] answers = {
{"2011", "2010", "2012"},
{"Alliance", "Orange Esports", "NAVI"},
{"Ancient Apparition", "Crystal Maiden", "Lich"},
{"2500 HP 1500 MP", "2000 HP 1500 MP", "2500 HP 2000 MP"},
{"Dark Willow", "Terrorblade", "Legion Commander"}
};
public static int[] generateSequence(int length) {
boolean[] used = new boolean[length];
int[] sequence = new int[length];
int size = 0;
Random random = new Random();
while (size < length) {
int i = random.nextInt(length);
if (!used[i]) {
sequence[size++] = i;
used[i] = true;
}
}
return sequence;
}
public static void startQuiz() {
int[] questionsSequence = generateSequence(questions.length);
int score = 0;
Scanner in = new Scanner(System.in);
System.out.println("DOTA 2 QUIZ");
for (int question : questionsSequence) {
System.out.println("\n" + questions[question]);
int[] answerSequence = generateSequence(answers[question].length);
for (int j = 0; j < answerSequence.length; j++) {
System.out.println(j + 1 + ". " + answers[question][answerSequence[j]]);
}
System.out.println("Answer:");
int answerIndex = Integer.parseInt(in.nextLine());
if (answers[question][0].equals(answers[question][answerSequence[answerIndex - 1]])) {
score++;
}
}
System.out.println("Your score is " + score);
}
public static void main(String[] args) {
startQuiz();
}
}
Comments
Leave a comment