import javax.swing.;
import java.awt.;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
public class MathQuizApp extends JFrame {
private static final int QUIZ_TIME = 3 * 60; // 3
private static final int QUESTION_COUNT = 40; // Ŀ
private Random rand = new Random();
private HashMap<String, Integer> questions = new HashMap<>();
private JTextField[] answerFields;
private Timer timer;
private JLabel timerLabel;
private int remainingTime;
public MathQuizApp() {
setTitle("Math Quiz");
setLayout(new GridLayout(QUESTION_COUNT + 2, 1));
setSize(400, 800);
setDefaultCloseOperation(EXIT_ON_CLOSE);
timerLabel = new JLabel("Time left: 3:00", SwingConstants.CENTER);
timerLabel.setFont(new Font("Arial", Font.BOLD, 30));
add(timerLabel);
answerFields = new JTextField[QUESTION_COUNT];
generateQuestions();
displayQuestions();
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(e -> checkAnswers());
add(submitButton);
startQuiz();
setVisible(true);
}
private void startQuiz() {
remainingTime = QUIZ_TIME;
timer = new Timer(1000, e -> {
remainingTime--;
timerLabel.setText(" ʱ: " + formatTime(remainingTime));
if (remainingTime <= 0) endQuiz();
});
timer.start();
}
private void generateQuestions() {
HashSet<String> generatedQuestions = new HashSet<>();
while (generatedQuestions.size() < QUESTION_COUNT) {
int num1 = rand.nextInt(51);
int num2 = rand.nextInt(51);
char op = "+-*/".charAt(rand.nextInt(4));
String question = "";
switch (op) {
case '+':
question = String.format("%d + %d =", num1, num2);
if (generatedQuestions.add(question))
questions.put(question, num1 + num2);
break;
case '-':
if (num1 >= num2) {
question = String.format("%d - %d =", num1, num2);
if (generatedQuestions.add(question))
questions.put(question, num1 - num2);
}
break;
case '*':
if (num1 * num2 < 1000) {
question = String.format("%d * %d =", num1, num2);
if (generatedQuestions.add(question))
questions.put(question, num1 * num2);
}
break;
case '/':
if (num2 != 0 && (num1 % num2 == 0)) {
question = String.format("%d / %d =", num1, num2);
if (generatedQuestions.add(question))
questions.put(question, num1 / num2);
}
break;
}
}
}
private void displayQuestions() {
for (int i = 0; i < QUESTION_COUNT; i++) {
String question = (String) questions.keySet().toArray()[i];
answerFields[i] = new JTextField(10);
answerFields[i].setPreferredSize(new Dimension(60, 25));
answerFields[i].setHorizontalAlignment(JTextField.CENTER);
JPanel linePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
linePanel.add(new JLabel(question + " "));
linePanel.add(answerFields[i]);
add(linePanel);
}
}
private void checkAnswers() {
int score = 0;
int wrongCount = 0;
for (int i = 0; i < QUESTION_COUNT; i++) {
String question = (String) questions.keySet().toArray()[i];
int correctAnswer = questions.get(question);
int userAnswer;
try {
userAnswer = Integer.parseInt(answerFields[i].getText());
} catch (NumberFormatException e) {
userAnswer = Integer.MIN_VALUE;
}
if (userAnswer == correctAnswer) {
score++;
} else {
wrongCount++;
}
}
double accuracy = (double) score / QUESTION_COUNT * 100;
String resultMessage = String.format("Score: %d/%d\nWrong Count: %d\nAccuracy: %.2f%%",
score, QUESTION_COUNT, wrongCount, accuracy);
JOptionPane.showMessageDialog(this, resultMessage);
System.exit(0);
}
private void endQuiz() {
timer.stop();
JOptionPane.showMessageDialog(this, "Time's up! Please submit your answers.");
checkAnswers();
}
private String formatTime(int seconds) {
int minutes = seconds / 60;
int secs = seconds % 60;
return String.format("%02d:%02d", minutes, secs);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(MathQuizApp::new);
}
}
标签:2024.10,String,num2,int,question,private,生成,算数,new From: https://www.cnblogs.com/yangsongduo/p/18459808