首页 > 其他分享 >12月20日

12月20日

时间:2023-12-21 23:14:56浏览次数:17  
标签:12 operand1 operand2 random nextInt new 20 append

今天用javaswing写了一个随机生成计算题的题目

主程序

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MathExerciseGenerator();
            }
        });
    }
}

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

public class MathExerciseGenerator extends JFrame {
    private JComboBox<String> operationComboBox;
    private JTextArea exerciseArea, resultArea;
    private JTextField answerField;

    public MathExerciseGenerator() {
        // 设置窗体属性
        setTitle("BYD");
        setSize(600, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建组件
        operationComboBox = new JComboBox<>(new String[]{"10以内加法", "10以内减法", "20以内加法", "20以内减法", "100以内加法", "100以内减法","100以内乘法","100以内除法"});
        exerciseArea = new JTextArea(10, 30);
        resultArea = new JTextArea(10, 30);
        resultArea.setEditable(false);
        answerField = new JTextField(20);

        JButton generateButton = new JButton("出题");
        JButton checkButton = new JButton("评卷");
        JButton showAnswerButton = new JButton("答案");
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                generateExercises();
            }
        });
        checkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                checkAnswers();
            }
        });
        showAnswerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showAnswers();
            }
        });

        // 布局管理
        setLayout(new FlowLayout());

        add(new JLabel("题目类型:"));
        add(operationComboBox);
        add(generateButton);
        add(checkButton);
        add(showAnswerButton);
        add(new JScrollPane(exerciseArea));
        add(new JLabel("你的答案:"));
        add(answerField);
        add(new JScrollPane(resultArea));
        setVisible(true);
    }

    private void generateExercises() {
        String operation = (String) operationComboBox.getSelectedItem();
        StringBuilder exercises = new StringBuilder();

        Random random = new Random();
        int operand1, operand2;
        for (int i = 0; i < 20; i++) {
            switch (operation) {
                case "10以内加法":
                    operand1 = random.nextInt(10)+1;
                    operand2 = random.nextInt(11 - operand1);
                    exercises.append(operand1).append(" + ").append(operand2).append(" =\n");
                    break;
                case "10以内减法":
                    operand1 = random.nextInt(10)+1;
                    operand2 = random.nextInt(operand1 + 1);
                    exercises.append(operand1).append(" - ").append(operand2).append(" =\n");
                    break;
                case "20以内加法":
                    operand1 = random.nextInt(20)+1;
                    operand2 = random.nextInt(21 - operand1);
                    exercises.append(operand1).append(" + ").append(operand2).append(" =\n");
                    break;
                case "20以内减法":
                    operand1 = random.nextInt(20)+1;
                    operand2 = random.nextInt(operand1 + 1);
                    exercises.append(operand1).append(" - ").append(operand2).append(" =\n");
                    break;
                case "100以内加法":
                    operand1 = random.nextInt(100)+1;
                    operand2 = random.nextInt(101 - operand1);
                    exercises.append(operand1).append(" + ").append(operand2).append(" =\n");
                    break;
                case "100以内减法":
                    operand1 = random.nextInt(100)+1;
                    operand2 = random.nextInt(operand1 + 1);
                    exercises.append(operand1).append(" - ").append(operand2).append(" =\n");
                    break;
                case "100以内乘法":
                    operand1 = random.nextInt(100)+1;
                    operand2 = random.nextInt(operand1 + 1);
                    exercises.append(operand1).append(" * ").append(operand2).append(" =\n");
                    break;
                case "100以内除法":
                    operand1 = random.nextInt(100)+1;
                    operand2 = random.nextInt(operand1 + 1);
                    while(operand1%operand2!=0)
                    {
                        operand2 = random.nextInt(operand1 + 1);
                    }
                    exercises.append(operand1).append(" / ").append(operand2).append(" =\n");
                    break;
            }
        }

        exerciseArea.setText(exercises.toString());
        resultArea.setText("");
        answerField.setText("");
    }

    private void checkAnswers() {
        String[] answers = answerField.getText().split(" ");
        String[] exercises = exerciseArea.getText().split("\n");
        StringBuilder results = new StringBuilder();
        System.out.println(answers.length);
        System.out.println(exercises.length);
        if (answers.length != exercises.length) {
            JOptionPane.showMessageDialog(this, "请回答所有题目。", "提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        for (int i = 0; i < exercises.length; i++) {
            String userAnswer = answers[i].trim();
            String[] parts = exercises[i].split("\\s+");
            int operand1 = Integer.parseInt(parts[0]);
            int operand2 = Integer.parseInt(parts[2]);
            int correctAnswer = 0;

            switch (parts[1]) {
                case "+":
                    correctAnswer = operand1 + operand2;
                    break;
                case "-":
                    correctAnswer = operand1 - operand2;
                    break;
                case "*":
                    correctAnswer = operand1 * operand2;
                    break;
                case "/":
                    correctAnswer = operand1 / operand2;
                    break;
            }

            boolean isCorrect = userAnswer.equals(String.valueOf(correctAnswer));
            results.append(isCorrect ? "√" : "╳").append("\n");
        }

        resultArea.setText(results.toString());
    }

    private void showAnswers() {
        String[] exercises = exerciseArea.getText().split("\n");
        StringBuilder answers = new StringBuilder();

        for (String exercise : exercises) {
            String[] parts = exercise.split("\\s+");
            int operand1 = Integer.parseInt(parts[0]);
            int operand2 = Integer.parseInt(parts[2]);
            int correctAnswer = 0;

            switch (parts[1]) {
                case "+":
                    correctAnswer = operand1 + operand2;
                    break;
                case "-":
                    correctAnswer = operand1 - operand2;
                    break;
                case "*":
                    correctAnswer = operand1 * operand2;
                    break;
                case "/":
                    correctAnswer = operand1 / operand2;
                    break;
            }

            answers.append(correctAnswer).append("\n");
        }

        resultArea.setText("答案:\n" + answers.toString());
    }
}

用于以图形化界面的方式展示题目。

标签:12,operand1,operand2,random,nextInt,new,20,append
From: https://www.cnblogs.com/xuechenhao173/p/17920311.html

相关文章

  • 12月21日
    今天进行了篮球考试,考了投篮和上篮,应该能及格,下午去上了数据结构写了快速排序intPartition(SqListL,intlow,inthigh){L.elem[0]=L.elem[low];intpivotkey=L.elem[0];while(low<high){while(low<high&&L.elem[high]>=pivotkey)--high;L......
  • 2023秋季专题训练五(二分)F
    问题K:计算平均值最大子段可以想到的做法是先枚举区间长度,然后计算每一个符合的区间平均值,但是会超时(timeout),很明显是时间复杂度n^2考虑如何优化(当然一开始没想到,还是老师提醒了一波)(明明之前课上还做到过)(哭)如何在O(n)判断一个区间是否满足,除了前缀和加除法的方法,也可以将数......
  • 20231221
    软件需求与分析课堂测试十一—综合案例建模分析(100分)销售订货管理系统是ERP的源头,如何管控销售订单下达、评审、跟进,不光是从软件上做约束管理,同时要从工作流程规定上做规范。【开发目的】规范公司订单下达、评审业务流程,提高客户订单准时交货率。【适用范围】适用于公司订......
  • 12.21每日报告
    今天早上考完了试设计模式 最后一题适配器模式写成了观察者还是没看清题题中说明不改变原有代码的基础上,所以不应该是观察者是给他调用猫的接口所以应该是适配器模式,将一个接口转换成用户所希望的另一个接口,将原本不兼容的类一起工作晚上做软件构造的大实验学习JFinal......
  • 【pwn】[ZJCTF 2019]EasyHeap --fastbin攻击,house of spirit
    首先查看一下附件的保护情况可以看到,got表是可修改的状态接着看主函数的逻辑非常典型的菜单题,接着分析每一个函数的作用unsigned__int64create_heap(){inti;//[rsp+4h][rbp-1Ch]size_tsize;//[rsp+8h][rbp-18h]charbuf[8];//[rsp+10h][rbp-10h]BY......
  • 【洛谷 P1093】[NOIP2007 普及组] 奖学金 题解(结构体排序)
    [NOIP2007普及组]奖学金题目描述某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前名学生发奖学金。期末,每个学生都有门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规......
  • CF1912H Hypercatapult Commute记录
    题目链接:https://codeforces.com/problemset/problem/1912/H题意有\(n\)个城市,\(m\)个人。第\(i\)人想从城市\(a_i\)移动到\(b_i\)。每个城市每天可以使用至多一次传送胶囊,可以将任意数目的人从该城市传送到任意一个(相同的)其它城市。注意传送有时间顺序。求是否可以让......
  • 12.21
    快速排序intPartition(SqListL,intlow,inthigh){L.elem[0]=L.elem[low];intpivotkey=L.elem[0];while(low<high){while(low<high&&L.elem[high]>=pivotkey)high--;L.elem[low]=L.elem[high];while(low<high&&......
  • Microsoft Visio 2021专业版安装包软件下载安装教程
    Microsoftvisio2021,简称visio2021。这是一款专业的专业矢量绘图软件。visio2021不但新增了许许多多的功能,而且还优化了众多的界面性能,其一系列的改动就是为了给予用户们最直观、最便利的操作感体验。同时呢,软件的操作也是相当的简单,只要用户熟悉软件上方中的菜单栏,其菜单栏与大家......
  • 新火种AI成功协办2023广州国际人工智能展览会
    2023年12月20日-22日,由新火种协办的2023广州国际人工智能展览会在广州琶洲-保利世贸博览馆成功举行。本次大会汇聚众多知名企业、政府机构、科创平台、高等院校,集中展示前沿人工智能技术与应用成果,是中国人工智能领域最具影响力和前瞻性的展会之一。 作为本次展会的协办单位,新火种......