首页 > 其他分享 >每日总结-23.11.22

每日总结-23.11.22

时间:2023-11-23 22:22:58浏览次数:42  
标签:总结 23.11 num1 22 int private operator new num2

package kousuanti;


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

public class ArithmeticProgram extends JFrame {
    private JPanel contentPanel;
    private JLabel[] questionLabels;
    private JTextField[] answerFields;
    private JButton regenerateButton;
    private JButton submitButton;
    private JLabel resultLabel;

    public ArithmeticProgram() {
        contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(10, 2)); // 一行两个元素,题目和作答区域

        questionLabels = new JLabel[10];
        answerFields = new JTextField[10];

        for (int i = 0; i < 10; i++) {
            int num1 = generateRandomNumber();
            int num2 = generateRandomNumber();
            char operator = generateRandomOperator();
            int result = calculateResult(num1, num2, operator);

            JLabel questionLabel = new JLabel("第" + (i+1) + "题: " + num1 + " " + operator + " " + num2 + " = ");
            questionLabels[i] = questionLabel;
            JTextField answerField = new JTextField();
            answerFields[i] = answerField;
            contentPanel.add(questionLabel);
            contentPanel.add(answerField);
        }

        regenerateButton = new JButton("重置");
        submitButton = new JButton("提交");

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

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

        resultLabel = new JLabel();

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(regenerateButton);
        buttonPanel.add(submitButton);

        add(contentPanel, BorderLayout.CENTER);
        add(resultLabel, BorderLayout.SOUTH);
        add(buttonPanel, BorderLayout.NORTH);

        setTitle("口算");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private int generateRandomNumber() {
        Random random = new Random();
        return random.nextInt(60) + 1; // 生成1到10的随机数
    }

    private char generateRandomOperator() {
        Random random = new Random();
        int op = random.nextInt(2);
        return (op == 0) ? '+' : '-';
    }

    private int calculateResult(int num1, int num2, char operator) {
        if (operator == '+') {
            return num1 + num2;
        } else {
            return num1 - num2;
        }
    }

    private void regenerateQuestions() {
        for (int i = 0; i < 10; i++) {
            int num1 = generateRandomNumber();
            int num2 = generateRandomNumber();
            char operator = generateRandomOperator();
            questionLabels[i].setText("Question " + (i+1) + ": " + num1 + " " + operator + " " + num2 + " = ");
            answerFields[i].setText("");
        }
        resultLabel.setText("");
    }

    private void checkAnswers() {
        StringBuilder wrongAnswers = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            String userAnswer = answerFields[i].getText().trim();
            int num1 = Integer.parseInt(questionLabels[i].getText().split("\\s+")[1]);
            char operator = questionLabels[i].getText().charAt(questionLabels[i].getText().length()-5);
            int num2 = Integer.parseInt(questionLabels[i].getText().split("\\s+")[3]);
            int correctResult = calculateResult(num1, num2, operator);
            if (!userAnswer.isEmpty() && Integer.parseInt(userAnswer) != correctResult) {
                wrongAnswers.append("Question ").append(i + 1).append(", ");
            }
        }
        if (wrongAnswers.length() == 0) {
            resultLabel.setText("All answers are correct!");
        } else {
            resultLabel.setText("Wrong answers: " + wrongAnswers.toString());
        }
    }

    public static void main(String[] args) {
        new ArithmeticProgram();
    }
}

 

标签:总结,23.11,num1,22,int,private,operator,new,num2
From: https://www.cnblogs.com/laobing6214/p/17852645.html

相关文章

  • 20231122
    2023/11/231798C-CandyStore只能说gcd,lcm的题目还是练少了,一些性质都不知道。对于一段可以用同一个标签的区间,我们知道他们的c是一样的,c=di*bi,每一个物品的bi固定,那么c就一定是lcm{bi..bn},因为c要能整除任何一个bi。然后我们来看可以自己决定的di,ai%di==0.而每一个物品的di=......
  • 每日总结-23.22.23
    packagekousuanti;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTextField;imp......
  • 《最简单的图形与最复杂的信息》总结
    绘制图形的核心原则真实刻画数据表现,不能夸张与缩小数据表现。一句话,还原数据本来的样子。图形的组成标题,坐标轴,图例(可选),数据曲线1.坐标轴a.选择合适的坐标刻度,尽可能让数据曲线占到图形的2/3Y轴的刻度不宜过小于过大,负责对于折线图表现变化过于夸张b.竖直条形图需要有零基......
  • 2023-2024 20232319《网络空间安全导论》第2周学习总结
    思维导图教材学习过程中的问题和解决过程问题一:sm2算法和sm4算法是对称算法还是非对称算法?答案:sm2属于非对称算法,sm4属于对称算法。问题一解决方案:询问chatgpt。问题二:区块链技术与密码学的关系答案:区块链技术与密码学有着密切的关系,密码学是区块链技术的基础之一。以下是......
  • 2023.11.23——每日总结
    学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.JavaGUI2.会话跟踪技术明日计划:学习......
  • 每日总结
    今日收获完成了软件设计的作业;被王老师挑了挺多的错误出来的--erp;但也算是顺利通过了吧~~~明天预计希望明天上午的测试顺利通过奥~~~然后,真的希望顺利通过!!!......
  • 11.23每日总结
    /AbstractChatroom.javapackageorg.example.test017;importjavax.swing.plaf.basic.BasicTreeUI;publicabstractclassAbstractChatroom{publicabstractvoidregister(Membermember);publicabstractvoidsendText(Stringfrom,Stringto,Stringme......
  • 2023-2024-1 20232421邓锴 《网络空间安全导论》 第2周学习总结
    教材学习总结了解密码的来源与发展ENIGMA机的构造以及机理信息论的基本观点了解现代密码学发展以及面临的挑战密码学理论基础(数学知识)密码学的新进展以及未来方向思维导图教材学习中的问题和解决问题问题1:ENIGMA机的构造以及机理了解不清问题1解决办法:查询图片→查找......
  • 虚拟机linux系统配置安装redis(每日总结11.23)
    1.进入到root模式下cd/export/server/2.下载wgethttp://download.redis.io/releases/redis-5.0.7.tar.gz3.解压tar-zvxfredis-5.0.7.tar.gz4.把文件移动到redis文件夹里mvredis-5.0.7/export/server/redis5.进入redis文件夹里cdredis6.编译m......
  • 2023-2024-1 20231329《计算机基础与程序设计》第9周学习总结
    作业信息这个作业属于哪个课程https://edu.cnblogs.com/campus/besti/2023-2024-1-CFAP这个作业要求在哪里https://www.cnblogs.com/rocedu/p/9577842.html#WEEK09这个作业的目标计算机科学概论第10,11章并完成云班课测试《C语言程序设计》第8章并完成云班课测试......