首页 > 其他分享 >今日总结

今日总结

时间:2024-10-20 23:34:01浏览次数:6  
标签:总结 operands int private problemPanel add new 今日

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、可以同时定义小学二年级口算题、小学三年级口算题、小学四年级口算题。

标签:总结,operands,int,private,problemPanel,add,new,今日
From: https://www.cnblogs.com/hy-tes/p/18488173

相关文章

  • CNVD漏洞和证书挖掘经验总结
    前言        本篇文章主要是分享一下本人挖掘CVND漏洞碰到的一些问题,根据过往成功归档的漏洞和未归档的漏洞总结出的经验,也确实给审核的大佬们添了很多麻烦(主要真的没人教一下,闷着头尝试犯了好很多错误,希望各位以后交一个通过一个。当然,也一定要注意测试资产的范围、......
  • 矩阵的秩性质总结
    矩阵的秩用法实在过于灵活,写篇随笔记录一下。矩阵的秩定义矩阵的秩常见定义有以下两种:非零子式的最高阶数。行(列)向量空间的极大无关组向量个数。矩阵的秩基本性质从定义出发不难得到以下性质:\(0\ler(A)\le\min(m,n)\)。\(r(A^T)=r(A)\)。\(r(kA)=r(A)\),要求\(k\n......
  • 《计算机基础与程序设计》第四周学习总结
    学期(2024-2025-1)学号(20241412)《计算机基础与程序设计》第四周学习总结作业信息这个作业属于哪个课程2024-2025-1-计算机基础与程序设计https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP这个作业要求在哪里2024-2025-1计算机基础与程序设计第一周作业https:......
  • 2024-2025 20241318 《计算机基础与程序设计》第四周学习总结
    这个作业属于哪个课程https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP这个作业要求在哪里https://www.cnblogs.com/rocedu/p/9577842.html#WEEK04这个作业的目标自学教材计算机科学概论(第七版)第4章,第5章并完成云班课测试《C语言程序设计》第3章并完成......
  • 2024-2025-1 20241419《计算机基础与程序设计》第四周学习总结
    作业信息课程要求目标:门电路组合电路,逻辑电路冯诺依曼结构CPU,内存,IO管理嵌入式系统,并行结构物理安全作业正文教程学习内容总结:数字电路:门电路:实现逻辑运算的单元电路,包括与、或、非等。组合电路:输出仅与当前输入有关的数字电路。逻辑电路:由逻辑门组成,输入输出信号......
  • 2024-2025-1 20241329 《计算机基础与程序设计》第四周学习总结
    作业信息作业归属课程:https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP作业要求:https://www.cnblogs.com/rocedu/p/9577842.html#WEEK04作业目标:门电路;组合电路,逻辑电路;冯诺依曼结构;CPU,内存,IO管理;嵌入式系统,并行结构;物理安全作业正文:https://www.cnblogs.com/incamelli......
  • PHP 正则表达式 修正符【m s x e ? (?i)】内部修正符 贪婪模式 后向引用 断言【总结篇
    1.正则表达式修正符在PHP中,正则表达式中的修正符(modifier)可以改变模式的行为,使得其功能更加灵活。1.m修正符(多行模式)作用:在多行模式下,^和$元字符除了匹配整个字符串的开头和结尾外,还可以匹配每一行的开头和结尾。举例: "Hello\nWorld",当使用/^World/m时,^会匹配"W......
  • #2024-2025-1学号20241309《计算机基础与程序设计》第四周学习总结
    作业信息这个作业属于哪个课程2024-2025-1-计算机基础与程序设计这个作业要求在哪里2024-2025-1计算机基础与程序设计第四周作业这个作业的目标|作业正文|2024-2025-1学号20241309《计算机基础与程序设计》第四周学习总结教材学习内容总结《计算机科学概论》......
  • 2024-2025-1 20241312 《计算机基础与程序设计》第4周学习总结
    作业信息|这个作业属于哪个课程|<班级的链接>(如2024-2025-1-计算机基础与程序设计)||这个作业要求在哪里|<作业要求的链接>(如2024-2025-1计算机基础与程序设计第四周作业||这个作业的目标|门电路组合电路,逻辑电路冯诺依曼结构CPU,内存,IO管理嵌入式系统,并行结构物理安全||作业......
  • 2024-2025-1 20241308 《计算机基础与程序设计》第四周学习总结
    作业信息这个作业属于哪个课程 https://edu.cnblogs.com/campus/besti/2024-2025-1-CFAP这个作业要求在哪里 https://www.cnblogs.com/rocedu/p/9577842.html#WEEK04这个作业的目标 <门电路组合电路,逻辑电路冯诺依曼结构CPU,内存,IO管理嵌入式系统,并行结构物理安全>作业正......