制作了一个验证码生成器;
点击查看代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class SimpleMathQuiz extends JFrame {
private static final int NUM_QUESTIONS = 30; // 题目数量
private static final int TIME_LIMIT = 60; // 60秒时间限制
private JTextArea questionArea; // 显示题目的文本区域
private JTextField answerField; // 用户输入答案的文本框
private JButton submitButton; // 提交答案按钮
private JLabel timerLabel; // 显示剩余时间的标签
private JLabel scoreLabel; // 显示分数和错误数的标签
private int currentQuestionIndex = 0; // 当前题目索引
private int score = 0; // 得分
private int wrongCount = 0; // 错误次数
private Question[] questions; // 题目数组
private Timer timer; // 用于计时
public SimpleMathQuiz() {
setTitle("简单数学测验");
setSize(400, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
questionArea = new JTextArea();
questionArea.setEditable(false);
questionArea.setLineWrap(true);
questionArea.setWrapStyleWord(true);
add(new JScrollPane(questionArea), BorderLayout.CENTER);
JPanel inputPanel = new JPanel();
answerField = new JTextField(10);
submitButton = new JButton("提交答案");
timerLabel = new JLabel("剩余时间: " + TIME_LIMIT);
scoreLabel = new JLabel("得分: 0 | 错误数: 0");
inputPanel.add(answerField);
inputPanel.add(submitButton);
inputPanel.add(timerLabel);
inputPanel.add(scoreLabel);
add(inputPanel, BorderLayout.SOUTH);
submitButton.addActionListener(new SubmitAnswerListener());
questions = QuestionGenerator.generateQuestions(NUM_QUESTIONS);
displayNextQuestion();
startTimer();
setVisible(true);
}
private void displayNextQuestion() {
if (currentQuestionIndex < NUM_QUESTIONS) {
questionArea.setText(questions[currentQuestionIndex].getQuestion());
answerField.setText("");
answerField.requestFocus();
} else {
endQuiz();
}
}
private void startTimer() {
timer = new Timer(1000, new ActionListener() {
private int timeLeft = TIME_LIMIT;
@Override
public void actionPerformed(ActionEvent e) {
if (timeLeft > 0) {
timeLeft--;
timerLabel.setText("剩余时间: " + timeLeft);
} else {
((Timer) e.getSource()).stop();
endQuiz();
}
}
});
timer.start();
}
private void endQuiz() {
timer.stop();
double errorRate = (double) wrongCount / NUM_QUESTIONS * 100;
JOptionPane.showMessageDialog(this, "测验结束!您的得分是: " + score + "/" + NUM_QUESTIONS + "\n错误率: " + String.format("%.2f", errorRate) + "%");
System.exit(0);
}
private class SubmitAnswerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
int answer = Integer.parseInt(answerField.getText());
if (answer == questions[currentQuestionIndex].getAnswer()) {
score++;
} else {
wrongCount++;
}
scoreLabel.setText("得分: " + score + " | 错误数: " + wrongCount);
currentQuestionIndex++;
displayNextQuestion();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(SimpleMathQuiz.this, "请输入有效的数字!");
}
}
}
private static class Question {
private final String question; // 题目字符串
private final int answer; // 答案
public Question(String question, int answer) {
this.question = question;
this.answer = answer;
}
public String getQuestion() {
return question;
}
public int getAnswer() {
return answer;
}
}
private static class QuestionGenerator {
public static Question[] generateQuestions(int numberOfQuestions) {
Question[] questions = new Question[numberOfQuestions];
Random random = new Random();
for (int i = 0; i < numberOfQuestions; i++) {
int a = random.nextInt(100); // 随机生成0到99的整数
int b = random.nextInt(100); // 随机生成0到99的整数
int operation = random.nextInt(4); // 随机选择运算符
switch (operation) {
case 0: // 加法
questions[i] = new Question(a + " + " + b, a + b);
break;
case 1: // 减法
if (a >= b) {
questions[i] = new Question(a + " - " + b, a - b);
} else {
i--; // 重新生成题目
}
break;
case 2: // 乘法
if (a * b < 10000) {
questions[i] = new Question(a + " * " + b, a * b);
} else {
i--; // 重新生成题目
}
break;
case 3: // 除法
if (b != 0 && a % b == 0) {
questions[i] = new Question(a + " / " + b, a / b);
} else {
i--; // 重新生成题目
}
break;
}
}
return questions;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(SimpleMathQuiz::new); // 启动GUI
}
}