首页 > 其他分享 >口算题卡

口算题卡

时间:2024-10-14 15:48:35浏览次数:3  
标签:int private n1 new n2 append 算题

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

class Main extends JFrame {
private JTextArea questionArea;
private JTextField[] answerFields;
private JButton submitButton;
private JLabel resultLabel;
private int correctCount = 0;
private int wrongCount = 0;
private Random random = new Random();
private int remainingTime = 300; // 以秒为单位的剩余时间,修改为5分钟
private ArrayList questionList = new ArrayList<>();
private Date startTime;

public Main() {
    setTitle("数学小测验");
    setSize(600, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    questionArea = new JTextArea();
    generateAllQuestions();
    JScrollPane scrollPane = new JScrollPane(questionArea);
    panel.add(scrollPane, BorderLayout.CENTER);

    JPanel answerPanel = new JPanel();
    answerPanel.setLayout(new GridLayout(30, 2));
    answerFields = new JTextField[30];
    for (int i = 0; i < 30; i++) {
        answerFields[i] = new JTextField();
        answerPanel.add(new JLabel("第" + (i + 1) + "题答案:"));
        answerPanel.add(answerFields[i]);
    }
    panel.add(answerPanel, BorderLayout.SOUTH);

    submitButton = new JButton("提交答案");
    submitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            checkAnswers();
            Date endTime = new Date();
            long diff = endTime.getTime() - startTime.getTime();
            long diffSeconds = diff / 1000;
            long minutes = diffSeconds / 60;
            long seconds = diffSeconds % 60;
            JOptionPane.showMessageDialog(null, "你完成所有题目共用了 " + minutes + " 分钟 " + seconds + " 秒");
            showFullResults();
        }
    });

    resultLabel = new JLabel();
    panel.add(submitButton, BorderLayout.EAST);
    panel.add(resultLabel, BorderLayout.NORTH);

    add(panel);

    startTime = new Date();
}

private void generateAllQuestions() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 30; i++) {
        int n1 = random.nextInt(100);
        int n2 = random.nextInt(100);
        int choose = random.nextInt(4);
        String op;
        int expectedResult;
        switch (choose) {
            case 0:
                op = "+";
                expectedResult = n1 + n2;
                break;
            case 1:
                if (n1 < n2) {
                    int temp = n1;
                    n1 = n2;
                    n2 = temp;
                }
                op = "-";
                expectedResult = n1 - n2;
                break;
            case 2:
                // 确保乘法结果不超过三位数
                do {
                    n1 = random.nextInt(100);
                    n2 = random.nextInt(100);
                } while (n1 * n2 >= 1000);
                op = "*";
                expectedResult = n1 * n2;
                break;
            case 3:
                n2 = getDivisor(n1, random);
                op = "/";
                expectedResult = n1 / n2;
                break;
            default:
                op = "%";
                expectedResult = n1 % n2;
                break;
        }
        sb.append((i + 1)).append(". ").append(n1).append(" ").append(op).append(" ").append(n2).append(" = \n");
        Question question = new Question(n1 + " " + op + " " + n2 + " =", 0, expectedResult);
        questionList.add(question);
    }
    questionArea.setText(sb.toString());
}

private void checkAnswers() {
    correctCount = 0;
    wrongCount = 0;
    for (int i = 0; i < 30; i++) {
        int userAnswer;
        try {
            userAnswer = Integer.parseInt(answerFields[i].getText());
            if (userAnswer == questionList.get(i).getExpectedAnswer()) {
                correctCount++;
            } else {
                wrongCount++;
            }
        } catch (NumberFormatException ex) {
            wrongCount++;
        }
    }
}

private int getDivisor(int n1, Random random) {
    int divisor;
    do {
        divisor = random.nextInt(99)+1;
    } while (n1 % divisor!= 0);
    return divisor;
}

private void showFullResults() {
    double totalQuestions = 30.0;
    double correct = correctCount;
    double accuracy = (correct * 100 / totalQuestions);
    DecimalFormat df = new DecimalFormat("0.00");
    StringBuilder resultMessage = new StringBuilder();
    resultMessage.append("错题数:").append(wrongCount).append("\n");
    resultMessage.append("正确率:").append(df.format(accuracy)).append("%\n");
    resultMessage.append("题目\t\t用户答案\t正确答案\n");
    for (Question question : questionList) {
        int index = questionList.indexOf(question);
        resultMessage.append(question.getQuestion()).append("\t\t");
        resultMessage.append(answerFields[index].getText()).append("\t ");
        resultMessage.append(question.getExpectedAnswer()).append("\n");
    }
    JOptionPane.showMessageDialog(null, resultMessage.toString());
}


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Main quiz = new Main();
            quiz.setVisible(true);
        }
    });
}

}

class Question {
private String question;
private int userAnswer;
private int expectedAnswer;

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

public String getQuestion() {
    return question;
}

public int getUserAnswer() {
    return userAnswer;
}

public int getExpectedAnswer() {
    return expectedAnswer;
}

}

标签:int,private,n1,new,n2,append,算题
From: https://www.cnblogs.com/Yunyuzuiluo/p/18464377

相关文章

  • 关于出四则运算题的进阶可视化解答
    `importjavax.swing.;importjavax.swing.border.TitledBorder;importjava.awt.;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.HashSet;importjava.util.Random;importjava.util.Scanner;classQuizFrameextendsJ......
  • 结对项目——实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程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......
  • 实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标通过实现一个自动生成小学四则运算题目的命令行程序提高软件开发能力姓名李佳聪......
  • 结对项目:自动生成小学四则运算题目
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标结对实现一个自动生成小学四则运算题目的命令行程序项目一、项目开发人员以及仓库地......
  • 结对项目——小学四则运算题目自动生成器
    这个作业属于哪个课程<计科22级34班>这个作业要求在哪里<结对项目>这个作业的目标<实现一个自动生成小学四则运算题目的命令行程序(也可以用图像界面,具有相似功能)>团队成员<杨富国(3122004587)、李思柔(3222004638)>Github项目地址https://github.com/wWchao-111......
  • 实现一个自动生成小学四则运算题目的命令行程序
    这个作业属于哪个课程https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/这个作业要求在哪里https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230这个作业的目标实现一个自动生成小学四则运算题目的命令行程序项目成员本结对项目由--31220045......
  • 出四则运算题
    一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。代码如下:importjava.util.;//导入java.util中的所有包publicclassMathtitle{publicstaticvoidmain(String[]args){//输出30道题目for(inti=0;i<30;i++){System.out.pr......
  • 基于递归下降解析器的四则运算题生成器
    结对项目本次项目的GitHub位置:https://github.com/EIiasK/Eliask/tree/main/3122004566/Exercise_Generator项目成员及github地址郭人诵github地址:https://github.com/EIiasK/Eliask何其浚github地址:https://github.com/hugh143/hugh143这个作业属于哪个课程......