Created
September 30, 2023 21:37
-
-
Save pog5/8e553dda88732d0e65b45bb31220bc83 to your computer and use it in GitHub Desktop.
Randomized Quiz made in Java 17 using nothing but built in utils and a hashmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Quiz { | |
private static Map<String, String> getQuestions() { | |
Map<String, String> questions = new HashMap<>(); // create questions map | |
// questions.put("Question", "Answer"); | |
questions.put("What is the main benefit of using IOPSX?\n A. Increased performance \n B. Reduced latency \n C. Improved scalability \n D. All of the above", "b"); | |
questions.put("What is the full name of IOPSX?\n A. Input/Output Parallel Storage Accelerator\n B. Input/Output Parallel Storage Extension\n C. Input/Output Parallel Storage Exchange\n D. Input/Output Parallel Storage Extractor\n", "a"); | |
return questions; | |
} | |
public static void main(String[] args) { | |
final Scanner scanner = new Scanner(System.in); // Create scanner which will read user input | |
final Random generator = new Random(); // create random number generator | |
int score = 0; // Store the score | |
Map<String, String> questions = getQuestions(); | |
while (!questions.isEmpty()) { | |
Object[] questionmap = questions.keySet().toArray(); // convert questions map to array | |
Object randomQuestion = questionmap[generator.nextInt(questionmap.length)]; // get random question from array | |
String correctanswer = (questions.get(randomQuestion)); // get correct answer from random question | |
System.out.println(randomQuestion); | |
System.out.println("Enter your answer: "); | |
String answer = scanner.nextLine(); | |
if (answer.toLowerCase().contains(correctanswer.toLowerCase())) { | |
score++; | |
System.out.println("\nCorrect!\n"); | |
} else { | |
System.out.println("\nNope! The right answer was " + correctanswer + "\n"); | |
} | |
questions.remove(randomQuestion); // remove question from map to prevent repetition | |
} | |
// Calculate score and display results | |
System.out.println("Good job on finishing the test! Lets see how you did"); | |
System.out.println("You got (" + score + "/2) correct!\nThanks for playing!"); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment