首页 > 其他分享 >随机三十道四则运算

随机三十道四则运算

时间:2024-09-30 21:01:37浏览次数:9  
标签:COUNT return int 四则运算 QUESTION private 随机 三十 new

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

public class ArithmeticTestGUI extends JFrame {

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

private JTextField[] questionFields;
private JTextField[] answerFields;
private JLabel timerLabel;
private JLabel resultLabel;
private JButton submitButton;

private int currentQuestionIndex;
private int correctCount;
private int[] questions;
private int[] answers;
private Timer timer;
private int timeLeft;

public ArithmeticTestGUI() {
    setTitle("四则运算课堂测试");
    setLayout(new BorderLayout());

    initComponents();
    startTest();
}

private void initComponents() {
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new GridLayout(5, 6));

    questionFields = new JTextField[QUESTION_COUNT];
    answerFields = new JTextField[QUESTION_COUNT];

    for (int i = 0; i < QUESTION_COUNT; i++) {
        JPanel questionAnswerPanel = new JPanel(new BorderLayout());

        questionFields[i] = new JTextField(10);
        questionFields[i].setEditable(false);
        questionAnswerPanel.add(questionFields[i], BorderLayout.CENTER);

        JPanel answerPanel = new JPanel(new BorderLayout());
        answerFields[i] = new JTextField(5);
        answerPanel.add(new JLabel("答案: "), BorderLayout.WEST);
        answerPanel.add(answerFields[i], BorderLayout.CENTER);

        questionAnswerPanel.add(answerPanel, BorderLayout.EAST);

        topPanel.add(questionAnswerPanel);
    }

    JPanel bottomPanel = new JPanel();
    timerLabel = new JLabel("时间: " + TIME_LIMIT_SECONDS + " 秒");
    timerLabel.setFont(timerLabel.getFont().deriveFont(20f));
    bottomPanel.add(timerLabel);

    resultLabel = new JLabel();
    bottomPanel.add(resultLabel);

    submitButton = new JButton("提交");
    submitButton.setFont(submitButton.getFont().deriveFont(20f));
    submitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            checkAnswers();
        }
    });
    bottomPanel.add(submitButton);

    add(topPanel, BorderLayout.CENTER);
    add(bottomPanel, BorderLayout.SOUTH);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int width = (int) (screenSize.width * 0.8);
    int height = (int) (screenSize.height * 0.8);
    setSize(width, height);
}

private void startTest() {
    currentQuestionIndex = 0;
    correctCount = 0;
    questions = new int[QUESTION_COUNT * 3];
    answers = new int[QUESTION_COUNT];
    Random random = new Random();

    for (int i = 0; i < QUESTION_COUNT; i++) {
        int[] question = generateQuestion(random);
        questions[i * 3] = question[0];
        questions[i * 3 + 1] = question[1];
        questions[i * 3 + 2] = question[2];
    }

    timeLeft = TIME_LIMIT_SECONDS;
    updateTimerLabel();
    timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (timeLeft > 0) {
                timeLeft--;
                updateTimerLabel();
            } else {
                timer.stop();
                endTest();
            }
        }
    });
    timer.start();

    showNextQuestions();
}

private void updateTimerLabel() {
    timerLabel.setText("时间: " + timeLeft + " 秒");
}

private void showNextQuestions() {
    if (currentQuestionIndex < QUESTION_COUNT) {
        for (int i = 0; i < QUESTION_COUNT; i++) {
            if (i < currentQuestionIndex) {
                questionFields[i].setText("已回答");
                answerFields[i].setEditable(false);
            } else {
                int a = questions[i * 3];
                int b = questions[i * 3 + 1];
                int operator = questions[i * 3 + 2];
                String operatorSymbol = getOperatorSymbol(operator);
                questionFields[i].setText(a + " " + operatorSymbol + " " + b + " = ");
                answerFields[i].setText("");
                answerFields[i].requestFocus();
            }
        }
    } else {
        endTest();
    }
}

private void checkAnswers() {
    for (int i = currentQuestionIndex; i < QUESTION_COUNT; i++) {
        try {
            int userAnswer = Integer.parseInt(answerFields[i].getText());
            if (checkAnswer(questions[i * 3], questions[i * 3 + 1], questions[i * 3 + 2], userAnswer)) {
                correctCount++;
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的数字答案。");
            return;
        }
    }
    currentQuestionIndex = QUESTION_COUNT;
    endTest();
}

private void endTest() {
    timer.stop();
    double correctRate = (double) correctCount / QUESTION_COUNT * 100;
    resultLabel.setText("测试结束! 正确题数: " + correctCount + " 错误题数: " + (QUESTION_COUNT - correctCount) + " 正确率: " + correctRate + "%");
    submitButton.setEnabled(false);
    for (JTextField answerField : answerFields) {
        answerField.setEditable(false);
    }
}

private static int[] generateQuestion(Random random) {
    int a, b, operator;
    do {
        a = random.nextInt(89) + 10;
        b = random.nextInt(89) + 10;
        operator = random.nextInt(4);
    } while (!isValidQuestion(a, b, operator));

    return new int[]{a, b, operator};
}

private static boolean isValidQuestion(int a, int b, int operator) {
    switch (operator) {
        case 0:
            return true;
        case 1:
            return a >= b;
        case 2:
            return a * b < 1000;
        case 3:
            return b!= 0 && a % b == 0;
        default:
            return false;
    }
}

private static String getOperatorSymbol(int operator) {
    switch (operator) {
        case 0:
            return "+";
        case 1:
            return "-";
        case 2:
            return "*";
        case 3:
            return "/";
        default:
            return "";
    }
}

private static boolean checkAnswer(int a, int b, int operator, int answer) {
    int correctAnswer;
    switch (operator) {
        case 0:
            correctAnswer = a + b;
            break;
        case 1:
            correctAnswer = a - b;
            break;
        case 2:
            correctAnswer = a * b;
            break;
        case 3:
            correctAnswer = a / b;
            break;
        default:
            return false;
    }

    return answer == correctAnswer;
}

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

}

标签:COUNT,return,int,四则运算,QUESTION,private,随机,三十,new
From: https://www.cnblogs.com/bawangji666/p/18442432

相关文章

  • 四则运算2
    importjava.util.*;importjava.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.TimeUnit;publicclassArithmeticQuiz{privatestaticfinalintNUMBER_OF_QUESTIONS=10;privatestaticfinalin......
  • 基于Python+flask+MySQL+HTML的全国范围水质分析预测系统,可视化用echarts,预测算法随机
    1绪论近年来,水质监测系统的进步显著,这在全球环保意识不断提升的背景下尤为明显。大量资源被投入到水质监测技术的研发和应用中,以不断优化监测效能。水资源的保护及健康环境的维护,这种趋势旨在提升人们生活质量,确保优质的生活条件。通过持续不懈的努力,我们得以实现对水质的及......
  • 生信机器学习入门4 - 构建决策树(Decision Tree)和随机森林(Random Forest)分类器
    机器学习文章回顾生信机器学习入门1-数据预处理与线性回归(Linearregression)预测生信机器学习入门2-机器学习基本概念生信机器学习入门3-Scikit-Learn训练机器学习分类感知器生信机器学习入门4-scikit-learn训练逻辑回归(LR)模型和支持向量机(SVM)模型1.决策树(Dec......
  • Java实现随机抽奖的方法有哪些
    在Java中实现随机抽奖的方法,通常我们会使用java.util.Random类来生成随机数,然后基于这些随机数来选择中奖者。以下将给出几种常见的随机抽奖实现方式,包括从数组中抽取、从列表中抽取以及基于权重的抽奖方式。1.从数组中抽取importjava.util.Random;publicclassLottery......
  • 结对项目——实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/这个作业要求在哪里结对项目-作业-计科22级34班-班级博客-博客园(cnblogs.com)这个作业的目标结对项目——实现一个自动生成小学四则运算题目的命令行程序成员1陈奕奕32220045......
  • 结对项目:自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程计科22级12班这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-12/homework/13221姓名学号曾繁曦3122004841吴健民3122004667PSP表格一、流程图二、模块设计1.模块划分Main模块(Main.java)功能描述:程序的入......
  • 实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程计科22级12班这个作业要求在哪里结对项目这个作业的目标实现一个自动生成小学四则运算题目的命令行程序。一:詹洛熙3122004800GitHub链接(https://github.com/Zhannoi/-/tree/main)二:PSP表格PSP2.1PersonalSoftwarePr......
  • 结对项目——四则运算
    结对项目——四则运算这个作业属于哪个课程软工22级计科12班这个作业的要求在哪里作业要求这个作业的目标实现四则运算的结对编程项目成员姓名学号GitHub链接分工谭立业3122004365github项目功能的基本实现,博客的编写罗锴佳3122001905gith......
  • 实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标通过实现一个自动生成小学四则运算题目的命令行程序提高软件开发能力姓名李佳聪......
  • 基于python的四则运算自动生成的命令行程序
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标基于python的四则运算自动生成的命令行程序团队成员姚彬彬3122006324一.Githu......