10.15

可以设置题目总数,题目难度在2年级
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
private static volatile boolean timeUp = false; // 标记时间是否到
private static volatile boolean timedown = false; // 标记是否停止倒计时
private static JLabel timerLabel;// 用于显示倒计时的标签
private static JTextArea questionArea;// 用于显示当前的问题
private static JTextField answerField;//答案的文本框
private static JButton submitButton; //用户输入的答案
private static int currentQuestion = 0;//
private static int correct = 0;//正确题目
private static int wrong = 0;//错误题目
private static String[] questionStrings;
private static int[] answers;
private QuestionsManager questionsManager;
private QuestionsManager1 questionsManager1;
private static void countdown(int seconds) {
try {
while (seconds > 0 && !timedown) {
Thread.sleep(1000); // 每1秒更新一次
seconds--;
timerLabel.setText("剩余时间: " + seconds + " 秒");
}
if (!timedown) {
timeUp = true; // 时间到,设置标记
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
timerLabel.setText("剩余时间:"+seconds+"秒");
}
public Main() {
questionsManager = new QuestionsManager();
setTitle("十分钟内完成数学题"); // 设置窗口标题
setSize(400, 400); // 设置窗口大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭操作
setLocationRelativeTo(null); // 设置窗口在屏幕中间
// Initialize components
timerLabel = new JLabel("剩余时间: 600 秒", SwingConstants.CENTER);
questionArea = new JTextArea();
questionArea.setEditable(false); // 使问题区域不可编辑
answerField = new JTextField();
submitButton = new JButton("提交答案");

    // Layout setup
    setLayout(new BorderLayout());
    add(timerLabel, BorderLayout.NORTH);
    add(new JScrollPane(questionArea), BorderLayout.CENTER);
    add(answerField, BorderLayout.SOUTH);
    add(submitButton, BorderLayout.EAST);

    submitButton.addActionListener(new SubmitButtonListener());

    String input = JOptionPane.showInputDialog(this, "请输入要回答的题目数");
    int A = 0;

    try {
        A = Integer.parseInt(input); // 尝试将输入转换为整数
        setupQuestions(A); // 调用设置问题的函数
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(this, "请输入一个有效的整数!", "输入错误", JOptionPane.ERROR_MESSAGE);
        System.exit(0); // 输入无效时退出程序
    }

    // Start countdown thread
    Thread countdownThread = new Thread(() -> countdown(600)); // 600秒倒计时
    countdownThread.start();
}

private void setupQuestions(int A) {
    Random random = new Random();
    Set<String> questions = new HashSet<>();
    answers = new int[A]; // 存储正确答案
    questionStrings = new String[A]; // 存储题目字符串
    int a, b, c;
    // Generate questions
    for (int j = 0; j < A; ) {
        a = random.nextInt(100);
        b = random.nextInt(100);
        c = random.nextInt(4);
        String question = "";
        int answer = 0;
        switch (c) {
            case 0: // 加法
                question = a + "+" + b + "=";
                answer = a + b;
                break;
            case 1: // 减法
                if (a >= b) {
                    question = a + "-" + b + "=";
                    answer = a - b;
                } else {
                    continue;
                }
                break;
            case 2: // 乘法
                if (a * b < 1000) {
                    question = a + "*" + b + "=";
                    answer = a * b;
                } else {
                    continue;
                }
                break;
            case 3: // 除法
                if (a != 0 && b % a == 0) {
                    question = b + "/" + a + "=";
                    answer = b / a;
                } else {
                    continue;
                }
        }

        if (questions.add(question)) {
            questionStrings[j] = question; // 存储题目
            answers[j] = answer; // 存储答案
            j++;
        }
    }

    // Load the first question
    loadQuestion();
}

private void loadQuestion() {
    if (currentQuestion < questionStrings.length) {
        questionArea.setText(questionStrings[currentQuestion]);
        answerField.setText(""); // Clear previous answer
        answerField.requestFocus(); // Focus on answer field
    } else {
        endQuiz(); // No more questions, end quiz
    }
}

private void endQuiz() {
    // End the quiz
    timedown = true;
    JOptionPane.showMessageDialog(this, "答题结束!\n正确答案数量: " + correct + "\n错误答案数量: " + wrong + "\n正确率: " + String.format("%.2f%%", ((double)correct / (correct + wrong)) * 100), "结果", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0); // Close the application
}

private class SubmitButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (timeUp) {
            endQuiz(); // Time is up, end the quiz
            return;
        }
        try {
            int answer = Integer.parseInt(answerField.getText());
            if (answer == answers[currentQuestion]) {
                correct++; // 答对了
            } else {
                wrong++; // 答错了
            }
        } catch (NumberFormatException ex) {
            wrong++; // Invalid input counts as wrong
            JOptionPane.showMessageDialog(Main.this, "无效输入,请输入数字。", "无效输入", JOptionPane.WARNING_MESSAGE);
        }
        currentQuestion++; // Move to the next question
        loadQuestion(); // Load the next question
    }
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        Main mainFrame = new Main();
        mainFrame.setVisible(true); // 显示窗口
    });
}

}

posted @ 2024-10-15 23:02  曹明阳  阅读(27)  评论(0)    收藏  举报