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

10月12日记录

时间:2024-10-12 19:11:16浏览次数:11  
标签: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......
  • 【教学类-07-09】20241011《破译电话号码-图形篇(图形固定列不重复)》(中2班 有名字 有班
     背景需求每次带班,我都会做一套“家长手机号”的破译电话号码给孩子做。目前已经有三种类型1、小班的“描写家长号码”【教学类-26-01】20230321背诵家长电话号码-Python数字填空(中班偏数学和社会)-CSDN博客文章浏览阅读144次。【教学类-26-01】20230321背诵家长电话号......
  • 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\)。然后将......