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

10月8日记录

时间:2024-10-08 20:44:46浏览次数:1  
标签:10 记录 int Question private questions new public

制作了一个验证码生成器;

点击查看代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class SimpleMathQuiz extends JFrame {
    private static final int NUM_QUESTIONS = 30; // 题目数量
    private static final int TIME_LIMIT = 60; // 60秒时间限制

    private JTextArea questionArea; // 显示题目的文本区域
    private JTextField answerField; // 用户输入答案的文本框
    private JButton submitButton; // 提交答案按钮
    private JLabel timerLabel; // 显示剩余时间的标签
    private JLabel scoreLabel; // 显示分数和错误数的标签

    private int currentQuestionIndex = 0; // 当前题目索引
    private int score = 0; // 得分
    private int wrongCount = 0; // 错误次数
    private Question[] questions; // 题目数组
    private Timer timer; // 用于计时

    public SimpleMathQuiz() {
        setTitle("简单数学测验");
        setSize(400, 350);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        questionArea = new JTextArea();
        questionArea.setEditable(false);
        questionArea.setLineWrap(true);
        questionArea.setWrapStyleWord(true);
        add(new JScrollPane(questionArea), BorderLayout.CENTER);

        JPanel inputPanel = new JPanel();
        answerField = new JTextField(10);
        submitButton = new JButton("提交答案");
        timerLabel = new JLabel("剩余时间: " + TIME_LIMIT);
        scoreLabel = new JLabel("得分: 0 | 错误数: 0");

        inputPanel.add(answerField);
        inputPanel.add(submitButton);
        inputPanel.add(timerLabel);
        inputPanel.add(scoreLabel);
        add(inputPanel, BorderLayout.SOUTH);

        submitButton.addActionListener(new SubmitAnswerListener());

        questions = QuestionGenerator.generateQuestions(NUM_QUESTIONS);
        displayNextQuestion();
        startTimer();

        setVisible(true);
    }

    private void displayNextQuestion() {
        if (currentQuestionIndex < NUM_QUESTIONS) {
            questionArea.setText(questions[currentQuestionIndex].getQuestion());
            answerField.setText("");
            answerField.requestFocus();
        } else {
            endQuiz();
        }
    }

    private void startTimer() {
        timer = new Timer(1000, new ActionListener() {
            private int timeLeft = TIME_LIMIT;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (timeLeft > 0) {
                    timeLeft--;
                    timerLabel.setText("剩余时间: " + timeLeft);
                } else {
                    ((Timer) e.getSource()).stop();
                    endQuiz();
                }
            }
        });
        timer.start();
    }

    private void endQuiz() {
        timer.stop();
        double errorRate = (double) wrongCount / NUM_QUESTIONS * 100;
        JOptionPane.showMessageDialog(this, "测验结束!您的得分是: " + score + "/" + NUM_QUESTIONS + "\n错误率: " + String.format("%.2f", errorRate) + "%");
        System.exit(0);
    }

    private class SubmitAnswerListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                int answer = Integer.parseInt(answerField.getText());
                if (answer == questions[currentQuestionIndex].getAnswer()) {
                    score++;
                } else {
                    wrongCount++;
                }
                scoreLabel.setText("得分: " + score + " | 错误数: " + wrongCount);
                currentQuestionIndex++;
                displayNextQuestion();
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(SimpleMathQuiz.this, "请输入有效的数字!");
            }
        }
    }

    private static class Question {
        private final String question; // 题目字符串
        private final int answer; // 答案

        public Question(String question, int answer) {
            this.question = question;
            this.answer = answer;
        }

        public String getQuestion() {
            return question;
        }

        public int getAnswer() {
            return answer;
        }
    }

    private static class QuestionGenerator {
        public static Question[] generateQuestions(int numberOfQuestions) {
            Question[] questions = new Question[numberOfQuestions];
            Random random = new Random();

            for (int i = 0; i < numberOfQuestions; i++) {
                int a = random.nextInt(100); // 随机生成0到99的整数
                int b = random.nextInt(100); // 随机生成0到99的整数
                int operation = random.nextInt(4); // 随机选择运算符

                switch (operation) {
                    case 0: // 加法
                        questions[i] = new Question(a + " + " + b, a + b);
                        break;
                    case 1: // 减法
                        if (a >= b) {
                            questions[i] = new Question(a + " - " + b, a - b);
                        } else {
                            i--; // 重新生成题目
                        }
                        break;
                    case 2: // 乘法
                        if (a * b < 10000) {
                            questions[i] = new Question(a + " * " + b, a * b);
                        } else {
                            i--; // 重新生成题目
                        }
                        break;
                    case 3: // 除法
                        if (b != 0 && a % b == 0) {
                            questions[i] = new Question(a + " / " + b, a / b);
                        } else {
                            i--; // 重新生成题目
                        }
                        break;
                }
            }
            return questions;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(SimpleMathQuiz::new); // 启动GUI
    }
}
这个程序能够自动生成图片验证码与刷新并且输入账号密码;

标签:10,记录,int,Question,private,questions,new,public
From: https://www.cnblogs.com/pygmy-killer-whale/p/18452506

相关文章

  • 10.8日
    在今日早上的工程实训中的电工基础实训中学习了不同的触电事故:电击和电伤,对于应对触电事故的措施和急救措施。Js是一种弱编程语言,其中对于声明变量,变量的数据类型有Number,String、boolean、undefined、null等,变量的数据类型取决于变量的值。其中声明变量有两种,let声明在目前使用......
  • 2024.10.8 鲜花
    好题蜂鸟(难忘今宵)传说中人类在远早住于黑暗的地下之遥派出了娇小的蜂鸟找到通往光明的隧道飞过了一座一座岛好想有一个地方落脚把一个一个梦制造会不会有人能够听到寻找太阳的梦自不量力说自己也变成太阳的念头有时候寂寞几乎扛不动咽在喉咙里无人诉说我们到底在......
  • 2018_10_31_02
    vuepress侧边栏module.exports={themeConfig:{sidebar:{//docs文件夹下面的accumulate文件夹文档中md文件书写的位置(命名随意)'/accumulate/':['/accumulate/',//accumulate文件夹的README.md不是下拉框形式{ti......
  • 2018_10_21_03
    box-flex使用说明1.box-flex属性box-flex主要让子容器针对父容器的宽度按一定规则进行划分<acticle><section>01</section><section>02</section><section>03</section></acticle>acticle{display:box;display:-moz-box;display......
  • 2018_10_21_02
    border-style属性定义及使用说明border-style属性设置一个元素的四个边框的样式。此属性可以有一到四个值。border-style:dottedsoliddoubledashed;值描述dotted上边框是点状solid右边框是实线double下边框是双线dashed左边框是虚线border-style......
  • 2018_10_21_01
    1px边框问题7种方法解决移动端Retina屏幕1px边框问题在retina屏中实现1pxborder效果下文出自7种方法解决移动端Retina屏幕1px边框问题造成边框变粗的原因其实这个原因很简单,因为css中的1px并不等于移动设备的1px,这些由于不同的手机有不同的像素密度。在......
  • 2018_10_29_02
    代码收集区分桌面环境与移动平台参考文章:https://segmentfault.com/a/1190000016760627<<<docs/.vuepress/components/js/2018_10_29_01/2018_10_29_01.jselement元素Y轴的scroll事件触发移动平台<<<docs/.vuepress/components/js/2018_10_29_02/html_scroll_mo......
  • 2018_10_28_01
    vscode配置vscode插件AtomOneDarkThemeAutoRenameTagvscode-language-babelChinese(Simplified)LanguagePackforVisualStudioCodeCodeSpellCheckerDockerESLintHopscotch(Official)IntelliJIDEAKeybindingsminapp-vscodePathIntellisensePHPDebu......
  • 2018_10_31_01
    gitcommit提交规范Commitmessage和Changelog编写指南规范你的commitmessage并且根据commit自动生成CHANGELOG.mdCommitmessage的格式主要摘选了基本格式与如何使用<type>(<scope>):<subject>//空一行<body>//空一行<footer>其中,Header是必需的,Bod......
  • 2018_10_22_02
    git~F.A.Q在git的一般使用中,如果发现错误的将不想提交的文件add进入index之后,想回退取消,则可以使用命令:gitresetHEAD<file>...,同时gitadd完毕之后,git也会做相应的提示,比如:引用#Changestobecommitted:#(use"gitresetHEAD<file>..."tounstage)##newfile:Test......