import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ArithmeticQuiz {
private static final int NUMBER_OF_QUESTIONS = 10;
private static final int TIME_LIMIT_PER_QUESTION = 30; // seconds
private static List
private static int correctAnswers = 0;
private static int wrongAnswers = 0;
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
generateQuestions();
startQuiz();
showResults();
}
private static void generateQuestions() {
Set<String> uniqueQuestions = new HashSet<>();
Random random = new Random();
while (questions.size() < NUMBER_OF_QUESTIONS) {
int type = random.nextInt(4); // 0: +, 1: -, 2: *, 3: /
int num1 = random.nextInt(100) + 1; // 1 to 100
int num2 = random.nextInt(100) + 1; // 1 to 100
int answer = 0;
String questionText = "";
switch (type) {
case 0: // Addition
answer = num1 + num2;
questionText = num1 + " + " + num2;
break;
case 1: // Subtraction
if (num1 >= num2) {
answer = num1 - num2;
questionText = num1 + " - " + num2;
}
break;
case 2: // Multiplication
answer = num1 * num2;
if (answer < 10000) {
questionText = num1 + " * " + num2;
}
break;
case 3: // Division
if (num2 != 0 && num1 % num2 == 0) {
answer = num1 / num2;
questionText = num1 + " / " + num2;
}
break;
}
if (!questionText.isEmpty() && uniqueQuestions.add(questionText)) {
questions.add(new ArithmeticQuestion(questionText, answer));
}
}
}
private static void startQuiz() {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
for (ArithmeticQuestion question : questions) {
System.out.println("Question: " + question.question);
executor.schedule(() -> {
System.out.println("Time's up! Moving to the next question.");
wrongAnswers++;
askQuestion(false, question);
}, TIME_LIMIT_PER_QUESTION, TimeUnit.SECONDS);
boolean answered = askQuestion(true, question);
if (answered) {
executor.shutdownNow();
executor = Executors.newScheduledThreadPool(1); // Create a new executor for the next question
}
}
executor.shutdownNow();
}
private static boolean askQuestion(boolean waitForAnswer, ArithmeticQuestion question) {
if (waitForAnswer) {
System.out.print("Your answer: ");
int userAnswer = scanner.nextInt();
if (userAnswer == question.answer) {
correctAnswers++;
System.out.println("Correct!");
} else {
wrongAnswers++;
System.out.println("Wrong! Correct answer was: " + question.answer);
}
return true; // User has answered
}
return false; // Time's up, user didn't answer
}
private static void showResults() {
System.out.println("\nQuiz Results:");
System.out.println("Correct Answers: " + correctAnswers);
System.out.println("Wrong Answers: " + wrongAnswers);
double accuracy = ((double) correctAnswers / (correctAnswers + wrongAnswers)) * 100;
System.out.printf("Accuracy: %.2f%%\n", accuracy);
}
}
class ArithmeticQuestion {
String question;
int answer;
ArithmeticQuestion(String question, int answer) {
this.question = question;
this.answer = answer;
}
}
标签:num1,num2,int,四则运算,question,static,answer From: https://www.cnblogs.com/bixi/p/18442083