首页 > 其他分享 >10月12日记录

10月12日记录

时间:2024-10-12 19:11:16浏览次数:10  
标签:10 12 return 记录 int private add new op

一个能够生成30道四则运算的程序,拥有可视化界面,计分是计算正确数量与错误数量;

点击查看代码
package RandomMathQuiz.java;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import java.util.List;
public class Quizapp extends JFrame {

    private static final int QUESTION_COUNT = 30;
    private static final int TIME_LIMIT_SECONDS = 60;

    private List<Question> questions;
    private int currentQuestionIndex = 0;
    private JLabel questionLabel;
    private JTextField answerField;
    private JButton submitButton;
    private JButton nextButton;
    private JLabel timerLabel;
    private JLabel resultLabel;
    private JLabel correctCountLabel;
    private JLabel incorrectCountLabel;
    private int correctCount = 0;
    private int incorrectCount = 0;

    private ScheduledExecutorService timerService;

    public Quizapp() {
        super("Math Quiz");

        setLayout(new FlowLayout());
        questionLabel = new JLabel("Question: ");
        answerField = new JTextField(10);
        submitButton = new JButton("Submit");
        nextButton = new JButton("Next");
        timerLabel = new JLabel("Time left: " + TIME_LIMIT_SECONDS + " seconds");
        resultLabel = new JLabel("");
        correctCountLabel = new JLabel("Correct: 0");
        incorrectCountLabel = new JLabel("Incorrect: 0");

        submitButton.addActionListener(new SubmitButtonListener());
        nextButton.addActionListener(new NextButtonListener());
        nextButton.setEnabled(false);

        add(questionLabel);
        add(answerField);
        add(submitButton);
        add(nextButton);
        add(timerLabel);
        add(resultLabel);
        add(correctCountLabel);
        add(incorrectCountLabel);

        setSize(400, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        questions = generateQuestions();
        updateQuestion();
        timerService = Executors.newScheduledThreadPool(1);
        timerService.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                int timeLeft = TIME_LIMIT_SECONDS - (currentQuestionIndex * (TIME_LIMIT_SECONDS / QUESTION_COUNT));
                timerLabel.setText("Time left: " + timeLeft + " seconds");
                if (timeLeft <= 0) {
                    nextQuestion();
                }
            }
        }, 0, (1000 * QUESTION_COUNT) / TIME_LIMIT_SECONDS, TimeUnit.MILLISECONDS);
    }

    private void updateQuestion() {
        if (currentQuestionIndex < questions.size()) {
            questionLabel.setText(questions.get(currentQuestionIndex).toString());
            answerField.setText("");
            answerField.requestFocus();
        } else {
            timerService.shutdownNow();
            submitButton.setEnabled(false);
            nextButton.setEnabled(false);
            resultLabel.setText("Quiz complete.");
            correctCountLabel.setText("Correct: " + correctCount);
            incorrectCountLabel.setText("Incorrect: " + incorrectCount);
        }
    }

    private void nextQuestion() {
        if (currentQuestionIndex < questions.size()) {
            currentQuestionIndex++;
            if (currentQuestionIndex == questions.size()) {
                nextButton.setEnabled(false);
            }
            updateQuestion();
        }
    }

    private List<Question> generateQuestions() {
        List<Question> questions = new ArrayList<>();
        Set<String> seen = new HashSet<>();
        while (questions.size() < QUESTION_COUNT) {
            int a = randomInt(1, 100);
            int b = randomInt(1, 100);
            char op = getValidOperation(a, b);
            if (seen.add(createQuestionKey(a, b, op))) {
                questions.add(new Question(a, b, op));
            }
        }
        return questions;
    }

    private int randomInt(int min, int max) {
        return (int) (Math.random() * (max - min + 1) + min);
    }

    private char getValidOperation(int a, int b) {
        if (a >= b && a - b >= 0) {
            return '-';
        } else if (a * b < 1000) {
            return '*';
        } else if (b != 0 && a % b == 0) {
            return '/';
        } else {
            return '+';
        }
    }

    private String createQuestionKey(int a, int b, char op) {
        return a + "-" + b + "-" + op;
    }

    private class SubmitButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int userAnswer = Integer.parseInt(answerField.getText());
            int correctAnswer = solve(questions.get(currentQuestionIndex));
            if (userAnswer == correctAnswer) {
                correctCount++;
                correctCountLabel.setText("Correct: " + correctCount);
                nextQuestion();
            } else {
                incorrectCount++;
                incorrectCountLabel.setText("Incorrect: " + incorrectCount);
                nextQuestion();
            }
        }
    }

    private class NextButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            nextQuestion();
        }
    }

    public static int solve(Question q) {
        switch (q.op) {
            case '+':
                return q.a + q.b;
            case '-':
                return q.a - q.b;
            case '*':
                return q.a * q.b;
            case '/':
                return q.a / q.b;
            default:
                throw new IllegalStateException("Unexpected value: " + q.op);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Quizapp app = new Quizapp();
            app.setVisible(true);
        });
    }

    static class Question {
        int a, b;
        char op;

        public Question(int a, int b, char op) {
            this.a = a;
            this.b = b;
            this.op = op;
        }

        @Override
        public String toString() {
            return a + " " + op + " " + b + " = ?";
        }
    }
}

标签:10,12,return,记录,int,private,add,new,op
From: https://www.cnblogs.com/pygmy-killer-whale/p/18461282

相关文章

  • 汉王语音王 1.0.22 | 同声传译,会议语音记录,实时翻译
    汉王语音王是一款完全免费的实时语音转文字软件,支持AI语音转写、实时语音转文字、导入音频转文字、同声传译、对话翻译等多种功能。识别准确率高,转写和翻译速度快,支持智能区分说话人、自动总结核心要点、拍录同步、PDF和Word格式导出等功能。大小:53.2M百度网盘:https://pa......
  • 20241012 模拟赛总结
    2024--梦熊&太戈--NOIP十三连测#12期望得分:100+40+28+50=218实际得分:70+30+28+60=188抽象得分。T1逆天,,花了很久在思考样例4是怎么构造的。对于每种面值的硬币,要凑的话最多只能选\(c_i=\dfrac{a_{i+1}}{a_i}-1\)个,否则的话就会被下一种面值的给替代掉,根据一直手......
  • 10.12
    似的有点惨。真实实力NOIP20+0+0+16=36pts。A.礼物赛时最简单的第一步没整出来,后面都想到了(\(s(c+x)|c(s+x)\)给他转化一下得:\(c(s+x)=k\timess(c+x),k\ge1\),类似解方程可得\(c=\frac{ksx}{x+s-ks}\)。若\(s\gex\),那么由\(x+s-ks>0\)得\(1\lek<2\),即\(k=1......
  • 2024.10.12总结
    本文于github博客同步更新你他妈管这个叫noip模拟赛?A:对于上述整除式的一组解\((c,s)\),在\(c\leqa\leqA\)且\(s\leqb\leqB\)时,会被统计入答案,因此它对答案的贡献为\((A-c-1)(B-s-1)\)。在\(s>x\)时,注意到\(\frac{s}{s+x}>\frac{1}{2}\),\(\frac{c}{c+......
  • 【教学类-34-09】20241010“圆角”凹凸拼图)中2班8K图纸绘画 自制A4拼图(3*4)
     背景需求早晨,21号第一个来园,我想给她描一下学号字帖,发现找不到她的字帖。我问“你是不是把21的字帖带回去了?”孩子点点头。难怪,这几天她做作业,都写出了自己的学号21,可能是家长看到了书包里的字帖,在家给她练过数字了! 感觉字帖挺有用的(* ̄︶ ̄)【教学类-23-02】2024092......
  • 【教学类-07-09】20241011《破译电话号码-图形篇(图形固定列不重复)》(中2班 有名字 有班
     背景需求每次带班,我都会做一套“家长手机号”的破译电话号码给孩子做。目前已经有三种类型1、小班的“描写家长号码”【教学类-26-01】20230321背诵家长电话号码-Python数字填空(中班偏数学和社会)-CSDN博客文章浏览阅读144次。【教学类-26-01】20230321背诵家长电话号......
  • 《DNK210使用指南 -CanMV版 V1.0》第二十九章 音频录制实验
    第二十九章音频录制实验1)实验平台:正点原子DNK210开发板2)章节摘自【正点原子】DNK210使用指南-CanMV版V1.03)购买链接:https://detail.tmall.com/item.htm?&id=7828013987504)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/ATK-DNK210.html5)正点原......
  • sql server 2012提示:评估期已过 的解决办法 附序列号
    sqlserver2012版本序列号如下:MICROSOFTSQLSERVER2012企业核心版激活码序列号:FH666-Y346V-7XFQ3-V69JM-RHW28MICROSOFTSQLSERVER2012商业智能版激活码序列号:HRV7T-DVTM4-V6XG8-P36T4-MRYT6MICROSOFTSQLSERVER2012开发版激活码序列号:YQWTX-G8T4R-QW4XX-BV......
  • 10.12 模拟赛
    题解A.选择排序粘过来题面的代码:for(inti=1;i<=n;i++){for(intj=1;j<=n;j++)if(a[i]<a[j])swap(a[i],a[j]);}考虑如何计算整个串的答案。首先暴力做一遍\(i=1\)。此时序列中的最大值一定会被交换到\(a_1\)。然后将......
  • 记录一篇 jvm内容:主要为 堆方向
    运行时数据区:Java虚拟机在执行Java程序的过程中会把它管理的内存划分成若干个不同的数据区域。根据《java虚拟机规范》的规定,Java虚拟机所管理的内存将会包括以下几个运行时数据区。 Java堆(JavaHea......