今日总结

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class ArithmeticProblem {
private int[] operands;
char[] operators;
private int result;

public ArithmeticProblem(int numOperands) {
    operands = new int[numOperands];
    operators = new char[numOperands - 1];
    generateProblem();
}

private void generateProblem() {
    Random random = new Random();
    for (int i = 0; i < operands.length; i++) {
        operands[i] = random.nextInt(100);
    }
    for (int i = 0; i < operators.length; i++) {
        int op = random.nextInt(4);
        switch (op) {
            case 0:
                operators[i] = '+';
                break;
            case 1:
                operators[i] = '-';
                break;
            case 2:
                operators[i] = '*';
                break;
            case 3:
                operators[i] = '/';
                break;
        }
    }
    result = calculateResult();
}

private int calculateResult() {
    int tempResult = operands[0];
    for (int i = 0; i < operators.length; i++) {
        switch (operators[i]) {
            case '+':
                tempResult += operands[i + 1];
                break;
            case '-':
                tempResult -= operands[i + 1];
                break;
            case '*':
                tempResult *= operands[i + 1];
                break;
            case '/':
                tempResult /= operands[i + 1];
                break;
        }
    }
    return tempResult;
}

public String getProblemString() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < operands.length; i++) {
        sb.append(operands[i]);
        if (i < operators.length) {
            sb.append(" ").append(operators[i]).append(" ");
        }
    }
    return sb.toString();
}

public int getResult() {
    return result;
}

}

class GradeTwoProblem extends ArithmeticProblem {

public GradeTwoProblem(int numOperands) {
    super(numOperands);
}

}

class GradeThreeProblem extends GradeTwoProblem {

public GradeThreeProblem(int numOperands) {
    super(numOperands);
}

}

class GradeFourProblem extends GradeThreeProblem {

public GradeFourProblem() {
    super(5);
    addParentheses();
}

private void addParentheses() {
    Random random = new Random();
    if (random.nextBoolean()) {
        int[] operands = new int[0];
        int start = random.nextInt(operands.length - 1);
        int end = start + random.nextInt(operands.length - start);
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        for (int i = start; i < end; i++) {
            sb.append(operands[i]).append(" ").append(operators[i]).append(" ");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(")");
        for (int i = end; i < operands.length; i++) {
            sb.append(operands[i]).append(" ").append(operators[i]).append(" ");
        }
        sb.deleteCharAt(sb.length() - 1);
        String problemString = sb.toString();
        operands = new int[problemString.length()];
        operators = new char[problemString.length()];
        int operandIndex = 0;
        int operatorIndex = 0;
        StringBuilder tempOperand = new StringBuilder();
        for (int i = 0; i < problemString.length(); i++) {
            char c = problemString.charAt(i);
            if (Character.isDigit(c)) {
                tempOperand.append(c);
            } else if (c == '+' || c == '-' || c == '*' || c == '/') {
                operands[operandIndex++] = Integer.parseInt(tempOperand.toString());
                tempOperand = new StringBuilder();
                operators[operatorIndex++] = c;
            } else if (c == '(' || c == ')') {
                // Ignore parentheses for now
            }
        }
        operands[operandIndex] = Integer.parseInt(tempOperand.toString());
    }
}

}

class ArithmeticTester {
private JFrame frame;
private JPanel panel;
private JTextField numProblemsField;
private JCheckBox hasMultiplicationCheckBox;
private JCheckBox hasDivisionCheckBox;
private JCheckBox hasParenthesesCheckBox;
private JButton generateButton;
private JButton submitButton;
private JComboBox gradeComboBox;
private JTextField numOperandsField;
private List problems;
private List answerFields;
private List wrongAnswers;
private int currentProblemIndex;

public ArithmeticTester() {
    frame = new JFrame("四则运算测试器");
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    numProblemsField = new JTextField(5);
    hasMultiplicationCheckBox = new JCheckBox("有乘法");
    hasDivisionCheckBox = new JCheckBox("有除法");
    hasParenthesesCheckBox = new JCheckBox("有括号");
    generateButton = new JButton("生成题目");
    submitButton = new JButton("提交答案");
    gradeComboBox = new JComboBox<>(new String[]{"二年级", "三年级", "四年级"});
    numOperandsField = new JTextField(3);
    problems = new ArrayList<>();
    answerFields = new ArrayList<>();
    wrongAnswers = new ArrayList<>();
    currentProblemIndex = 0;

    JPanel inputPanel = new JPanel();
    inputPanel.add(new JLabel("题目数量:"));
    inputPanel.add(numProblemsField);
    inputPanel.add(new JLabel("年级:"));
    inputPanel.add(gradeComboBox);
    inputPanel.add(new JLabel("操作数个数:"));
    inputPanel.add(numOperandsField);
    inputPanel.add(hasMultiplicationCheckBox);
    inputPanel.add(hasDivisionCheckBox);
    inputPanel.add(hasParenthesesCheckBox);
    inputPanel.add(generateButton);

    panel.add(inputPanel);

    generateButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            generateProblems();
        }
    });

    submitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            submitAnswers();
        }
    });

    JPanel bottomPanel = new JPanel();
    bottomPanel.add(submitButton);

    frame.add(panel, BorderLayout.CENTER);
    frame.add(bottomPanel, BorderLayout.SOUTH);
    frame.setSize(400, 300);
    frame.setVisible(true);
}

private void generateProblems() {
    int numProblems = Integer.parseInt(numProblemsField.getText());
    boolean hasMultiplication = hasMultiplicationCheckBox.isSelected();
    boolean hasDivision = hasDivisionCheckBox.isSelected();
    boolean hasParentheses = hasParenthesesCheckBox.isSelected();
    String selectedGrade = (String) gradeComboBox.getSelectedItem();
    int numOperands = Integer.parseInt(numOperandsField.getText());
    problems.clear();
    answerFields.clear();
    for (int i = 0; i < numProblems; i++) {
        ArithmeticProblem problem;
        if (selectedGrade.equals("四年级") && hasParentheses && i % 3 == 0) {
            problem = new GradeFourProblem();
        } else if (selectedGrade.equals("三年级") && i % 2 == 0) {
            problem = new GradeThreeProblem(numOperands);
        } else if (selectedGrade.equals("二年级")) {
            problem = new GradeTwoProblem(numOperands);
        } else {
            problem = new GradeTwoProblem(numOperands);
        }
        problems.add(problem);
        JTextField answerField = new JTextField(5);
        JPanel problemPanel = new JPanel();
        problemPanel.setLayout(new BoxLayout(problemPanel, BoxLayout.X_AXIS));
        problemPanel.add(Box.createHorizontalGlue());
        JLabel problemLabel = new JLabel(problem.getProblemString() + " =");
        problemPanel.add(problemLabel);
        problemPanel.add(answerField);
        problemPanel.add(Box.createHorizontalGlue());
        panel.add(problemPanel);
        answerFields.add(answerField);
    }
    panel.revalidate();
    panel.repaint();
}

private void submitAnswers() {
    int wrongCount = 0;
    for (int i = 0; i < problems.size(); i++) {
        int userAnswer = Integer.parseInt(answerFields.get(i).getText());
        int correctAnswer = problems.get(i).getResult();
        if (userAnswer!= correctAnswer) {
            wrongCount++;
            wrongAnswers.add(i);
        }
    }
    JOptionPane.showMessageDialog(frame, "你答对了 " + (problems.size() - wrongCount) + " 道题,答错了 " + wrongCount + " 道题。");
    if (wrongCount > 0) {
        JButton retryButton = new JButton("重练错题");
        JPanel retryPanel = new JPanel();
        retryPanel.add(retryButton);
        JOptionPane.showMessageDialog(frame, retryPanel, "重练错题", JOptionPane.PLAIN_MESSAGE);
        retryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                retryWrongAnswers();
            }
        });
    } else {
        JOptionPane.showMessageDialog(frame, "全部答对!");
    }
}

private void retryWrongAnswers() {
    problems.clear();
    answerFields.clear();
    int numProblems = wrongAnswers.size();
    boolean hasMultiplication = hasMultiplicationCheckBox.isSelected();
    boolean hasDivision = hasDivisionCheckBox.isSelected();
    boolean hasParentheses = hasParenthesesCheckBox.isSelected();
    String selectedGrade = (String) gradeComboBox.getSelectedItem();
    int numOperands = Integer.parseInt(numOperandsField.getText());
    JFrame newFrame = new JFrame("错题重练");
    JPanel newPanel = new JPanel();
    newPanel.setLayout(new BoxLayout(newPanel, BoxLayout.Y_AXIS));
    for (int index : wrongAnswers) {
        ArithmeticProblem problem;
        if (selectedGrade.equals("四年级") && hasParentheses && index % 3 == 0) {
            problem = new GradeFourProblem();
        } else if (selectedGrade.equals("三年级") && index % 2 == 0) {
            problem = new GradeThreeProblem(numOperands);
        } else if (selectedGrade.equals("二年级")) {
            problem = new GradeTwoProblem(numOperands);
        } else {
            problem = new GradeTwoProblem(numOperands);
        }
        problems.add(problem);
        JTextField answerField = new JTextField(5);
        JPanel problemPanel = new JPanel();
        problemPanel.setLayout(new BoxLayout(problemPanel, BoxLayout.X_AXIS));
        problemPanel.add(Box.createHorizontalGlue());
        JLabel problemLabel = new JLabel(problem.getProblemString() + " =");
        problemPanel.add(problemLabel);
        problemPanel.add(answerField);
        problemPanel.add(Box.createHorizontalGlue());
        newPanel.add(problemPanel);
        answerFields.add(answerField);
    }
    JButton newSubmitButton = new JButton("提交错题答案");
    newPanel.add(newSubmitButton);
    newSubmitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            submitWrongAnswers(newFrame, newPanel);
        }
    });
    newFrame.add(newPanel);
    newFrame.setSize(400, 300);
    newFrame.setVisible(true);
}

private void submitWrongAnswers(JFrame frame, JPanel panel) {
    int wrongCount = 0;
    for (int i = 0; i < problems.size(); i++) {
        int userAnswer = Integer.parseInt(answerFields.get(i).getText());
        int correctAnswer = problems.get(i).getResult();
        if (userAnswer!= correctAnswer) {
            wrongCount++;
        }
    }
    if (wrongCount > 0) {
        JOptionPane.showMessageDialog(frame, "你还有 " + wrongCount + " 道题答错了。请继续努力。");
    } else {
        JOptionPane.showMessageDialog(frame, "全部答对!恭喜你。");
    }
}

}

public class Main {
public static void main(String[] args) {
new ArithmeticTester();
}
}
实现了以下功能
1、可定制(数量):输入大的出题数量值.
2、操作数的个数、是否有乘除法、是否有括号(随机加入)、数值范围(确定操作数的取值范围);
3、实现错题集、错题重练并记录错题的次数功能。
4、可以同时定义小学二年级口算题、小学三年级口算题、小学四年级口算题。

posted @ 2024-10-20 23:36  落浅  阅读(5)  评论(0编辑  收藏  举报