A simple random question quiz in Java programming language. I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you. My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360. Please subscribe to my channel https://www.youtube.com/channel/UCOs-lpOoIeJoh6gpJthPoGg ================================================= You can buy my C++ book online atBeginner’s Guide To C++ ProgrammingYou can buy my book in introduction to computer networking at https://www.unlimitedbooksph.com/product-page/introduction-to-computer-networking Want to support my channel? GCash Account Jake Pomperada 09173084360 Paypal https://paypal.me/jakerpomperada Patreon https://www.patreon.com/jakerpomperada Thank you very much for your support.
Program Listing
import java.util.*;
public class Quiz {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Create a HashMap to store the questions and answers
HashMap<String, String> quiz = new HashMap<>();
quiz.put("What is the capital of Philippines?", "Manila");
quiz.put("What is fastest computer?", "Super Computer");
quiz.put("What is the chemical symbol for gold?", "Au");
quiz.put("What is the most common input device in the computer system?", "Keyboard");
quiz.put("What is the most common pointing device in the computer system?", "Mouse");
// Shuffle the questions
List<String> questions = new ArrayList<>(quiz.keySet());
Collections.shuffle(questions);
// Initialize score
int score = 0;
// Start the quiz
Scanner scanner = new Scanner(System.in);
System.out.print("\n\tRandom Quiz in Java\n\n");
for (String question : questions) {
System.out.println(question);
String answer = scanner.nextLine();
// Check the answer
if (answer.equalsIgnoreCase(quiz.get(question))) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Incorrect!");
}
System.out.println();
}
// Display final score
System.out.println("Quiz complete! Your score: " + score + "/" + quiz.size());
// Close the scanner
scanner.close();
}
}