10.14

实现了课上部分要求
QixunluGUI类

点击查看代码
package qixun;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;


public class QixunluGUI extends JFrame {
    private static final int TIME_LIMIT = 60;
    private int questionCount;
    private String[] questions;
    private double[] correctAnswers;
    private JTextField[] answerFields;
    private JLabel[] resultLabels;
    private JLabel timerLabel = new JLabel("剩余时间: " + TIME_LIMIT + "秒");
    private int correctCount = 0;
    private int timeRemaining = TIME_LIMIT;

    public QixunluGUI(int questionCount, int gradeLevel) {
        this.questionCount = questionCount;
        this.questions = new String[questionCount];
        this.correctAnswers = new double[questionCount];
        this.answerFields = new JTextField[questionCount];
        this.resultLabels = new JLabel[questionCount];

        setTitle("数学测验");
        setSize(500, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 生成问题
        QuestionGenerator generator = new QuestionGenerator(gradeLevel);
        for (int i = 0; i < questionCount; i++) {
            questions[i] = generator.getQuestion();
            correctAnswers[i] = generator.getAnswer(questions[i]);
        }

        // 创建一个面板用于问题布局,三道题一行
        JPanel questionPanel = new JPanel(new GridLayout((questionCount + 2) / 3, 4));
        for (int i = 0; i < questionCount; i++) {
            questionPanel.add(new JLabel("问题 " + (i + 1) + ": " + questions[i]));
            answerFields[i] = new JTextField(10);
            questionPanel.add(answerFields[i]);
            resultLabels[i] = new JLabel();
            questionPanel.add(resultLabels[i]);

            JButton checkButton = new JButton("提交");
            int finalI = i;
            checkButton.addActionListener(e -> submitSingleAnswer(finalI, resultLabels[finalI]));
            questionPanel.add(checkButton);
        }

        // 创建一个面板用于放置总提交按钮
        JPanel submitPanel = new JPanel(new FlowLayout());
        JButton finalSubmitButton = new JButton("提交所有答案");
        JLabel finalResultLabel = new JLabel();

        // 处理倒计时
        Timer timer = new Timer(1000, e -> {
            timeRemaining--;
            timerLabel.setText("剩余时间: " + timeRemaining + "秒");
            if (timeRemaining <= 0) {
                ((Timer) e.getSource()).stop();
                JOptionPane.showMessageDialog(null, "时间到!");
                submitAllAnswers(finalResultLabel);
            }
        });
        timer.start();

        // 设置总提交按钮事件
        finalSubmitButton.addActionListener(e -> {
            timer.stop();
            submitAllAnswers(finalResultLabel);
        });

        // 将提交按钮和结果标签加入提交面板
        submitPanel.add(finalSubmitButton);
        submitPanel.add(finalResultLabel);

        add(timerLabel, BorderLayout.NORTH);
        add(new JScrollPane(questionPanel), BorderLayout.CENTER); // 添加问题面板
        add(submitPanel, BorderLayout.SOUTH); // 提交按钮单独一行

        setVisible(true);
    }

    private void submitSingleAnswer(int index, JLabel resultLabel) {
        try {
            double userAnswer = Double.parseDouble(answerFields[index].getText());
            if (Math.abs(userAnswer - correctAnswers[index]) < 0.001) {
                correctCount++;
                resultLabel.setText("正确");
            } else {
                resultLabel.setText("错误,正确答案是: " + correctAnswers[index]);
            }
        } catch (NumberFormatException ex) {
            resultLabel.setText("请输入有效的数字");
        }
    }

    private void submitAllAnswers(JLabel finalResultLabel) {
        finalResultLabel.setText("总正确率: " + (correctCount / (double) questionCount * 100) + "%");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入题目数量: ");
        int questionCount = scanner.nextInt();
        System.out.print("请输入年级(2-4): ");
        int gradeLevel = scanner.nextInt();
        new QixunluGUI(questionCount, gradeLevel);
    }
}
WrongQuestionManager类
点击查看代码
package qixun;

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;


public class WrongQuestionManager {
    private Map<String, Integer> wrongQuestions = new HashMap<>();

    public void saveWrongQuestion(String question, String correctAnswer) {
        wrongQuestions.put(question, wrongQuestions.getOrDefault(question, 0) + 1);
        // 将错题保存到文件
    }

    public void retryWrongQuestions() {
        // 进行错题重练逻辑
    }
}

QuestionGenerator类 package qixun;

import java.util.;
import javax.script.
;

public class QuestionGenerator {
private int gradeLevel;
private int operandCount;
private boolean includeMulDiv;
private boolean includeBrackets;
private int range;
private Random random;

public QuestionGenerator(int gradeLevel) {
    this.gradeLevel = gradeLevel;
    this.random = new Random();
}

// 生成并返回一个数学题目(字符串)
public String getQuestion() {
    int num1 = random.nextInt(100); // 随机生成一个100以内的数字
    int num2 = random.nextInt(1, 101); // 随机生成另一个100以内的数字,确保不为0(用于除法)
    char operator = randomOperator();

    // 返回一个题目字符串
    return num1 + " " + operator + " " + num2;
}

// 根据生成的题目,计算并返回正确答案
public double getAnswer(String question) {
    // 从问题字符串中解析操作数和运算符
    String[] parts = question.split(" ");
    int num1 = Integer.parseInt(parts[0]);
    char operator = parts[1].charAt(0);
    int num2 = Integer.parseInt(parts[2]);

    switch (operator) {
        case '+':
            return num1 + num2;
        case '-':
            return num1 - num2;
        case '*':
            return num1 * num2;
        case '/':
            // 避免除以0
            return num2 != 0 ? num1 / (double) num2 : 0;
        default:
            throw new IllegalArgumentException("不支持的运算符: " + operator);
    }
}

// 随机生成一个运算符
private char randomOperator() {
    char[] operators = {'+', '-', '*', '/'};
    return operators[random.nextInt(operators.length)];
}
public QuestionGenerator(int gradeLevel, int operandCount, boolean includeMulDiv, boolean includeBrackets, int range) {
    this.gradeLevel = gradeLevel;
    this.operandCount = operandCount;
    this.includeMulDiv = includeMulDiv;
    this.includeBrackets = includeBrackets;
    this.range = range;
}

public String generateQuestion() {
    Random rand = new Random();
    List<Integer> operands = new ArrayList<>();
    List<String> operators = new ArrayList<>();

    for (int i = 0; i < operandCount; i++) {
        operands.add(rand.nextInt(range));
        if (i < operandCount - 1) {
            if (includeMulDiv) {
                operators.add(new String[]{"+", "-", "*", "/"}[rand.nextInt(4)]);
            } else {
                operators.add(new String[]{"+", "-"}[rand.nextInt(2)]);
            }
        }
    }

    if (includeBrackets) {
        // 随机插入括号逻辑
    }

    return buildExpression(operands, operators);
}

private String buildExpression(List<Integer> operands, List<String> operators) {
    // 构建表达式字符串
    StringBuilder expression = new StringBuilder();
    for (int i = 0; i < operands.size(); i++) {
        expression.append(operands.get(i));
        if (i < operators.size()) {
            expression.append(" ").append(operators.get(i)).append(" ");
        }
    }
    return expression.toString();
}

}

posted @ 2024-10-14 23:44  QixunQiu  阅读(10)  评论(0编辑  收藏  举报