首页 > 其他分享 >关于出四则运算题的进阶可视化解答

关于出四则运算题的进阶可视化解答

时间:2024-09-30 23:45:33浏览次数:10  
标签:进阶 num2 int 四则运算 private 可视化 operator problem num1

`import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;

class QuizFrame extends JFrame {
private HashSet generatedProblems;
private int correctAnswers;
private int totalProblems;
private int wrongAnswers;
private Random random;
private long startTime;
private long endTime;
private int countdownTime;
private JTextField[][] answerFields;
private JLabel timeLabel;
private JButton submitButton;

public QuizFrame() {
    setTitle("四则运算测试");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    random = new Random();
    generatedProblems = new HashSet<>();
    correctAnswers = 0;
    totalProblems = 30;
    wrongAnswers = 0;

    System.out.println("请输入做题时间");
    Scanner scanner = new Scanner(System.in);
    countdownTime = scanner.nextInt();
    if (countdownTime > 300 || countdownTime <= 0) {
        countdownTime = 60; // 默认一分钟
        System.out.println("输入时间不合法,采用默认一分钟倒计时。");
    }

    startTime = System.currentTimeMillis();
    endTime = startTime + countdownTime * 1000;

    JPanel mainPanel = new JPanel(new BorderLayout());

    JPanel problemPanel = new JPanel(new GridLayout(6, 5));
    problemPanel.setBorder(new TitledBorder("题目与答案"));

    answerFields = new JTextField[6][5];

    generateProblems();

    int index = 0;
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 5; j++) {
            if (index < totalProblems) {
                JLabel problemLabel = new JLabel(generatedProblems.toArray()[index].toString() + " = ");
                problemPanel.add(problemLabel);

                JTextField answerField = new JTextField();
                answerFields[i][j] = answerField;
                problemPanel.add(answerField);
                index++;
            }
        }
    }

    mainPanel.add(problemPanel, BorderLayout.CENTER);

    timeLabel = new JLabel();
    mainPanel.add(timeLabel, BorderLayout.NORTH);

    submitButton = new JButton("提交");
    submitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            checkAnswers();
        }
    });
    JPanel buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(submitButton);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    add(mainPanel);

    Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            long remainingTime = (endTime - System.currentTimeMillis()) / 1000;
            if (remainingTime >= 0) {
                timeLabel.setText("剩余时间:" + remainingTime + " 秒");
            } else {
                timeLabel.setText("时间到!");
                checkAnswers();
            }
        }
    });
    timer.start();
}

private void generateProblems() {
    while (generatedProblems.size() < totalProblems) {
        int num1 = random.nextInt(100);
        int num2;
        int operatorIndex = random.nextInt(4);
        int result;
        String operator;
        String problem;

        switch (operatorIndex) {
            case 0:
                operator = "+";
                num2 = random.nextInt(100 - num1);
                result = num1 + num2;
                problem = num1 + " " + operator + " " + num2;
                break;
            case 1:
                operator = "-";
                num2 = random.nextInt(num1 + 1);
                result = num1 - num2;
                problem = num1 + " " + operator + " " + num2;
                break;
            case 2:
                operator = "*";
                do {
                    num2 = random.nextInt(100);
                } while (num1 * num2 >= 1000);
                result = num1 * num2;
                problem = num1 + " " + operator + " " + num2;
                break;
            default:
                operator = "/";
                num2 = random.nextInt(100) + 1;
                while (num1 % num2!= 0) {
                    num2 = random.nextInt(100) + 1;
                }
                result = num1 / num2;
                problem = num1 + " " + operator + " " + num2;
                break;
        }

        if (!generatedProblems.contains(problem)) {
            generatedProblems.add(problem);
        }
    }
}

private void checkAnswers() {
    if (correctAnswers == 0 && wrongAnswers == 0) {
        int index = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 5; j++) {
                if (index < totalProblems) {
                    String problem = generatedProblems.toArray()[index].toString();
                    int expectedAnswer = calculateAnswer(problem);
                    try {
                        int userAnswer = Integer.parseInt(answerFields[i][j].getText());
                        if (userAnswer == expectedAnswer) {
                            correctAnswers++;
                        } else {
                            wrongAnswers++;
                        }
                    } catch (NumberFormatException ex) {
                        wrongAnswers++;
                    }
                    index++;
                }
            }
        }
        double accuracy = (double) correctAnswers / totalProblems * 100;
        int option = JOptionPane.showConfirmDialog(this, "答题结束。\n错题数:" + wrongAnswers + "\n正确率:" + accuracy + "%", "结果", JOptionPane.OK_CANCEL_OPTION);
        if (option == JOptionPane.OK_OPTION) {
            dispose();
        }
    }
}

private int calculateAnswer(String problem) {
    String[] parts = problem.split(" ");
    int num1 = Integer.parseInt(parts[0]);
    int num2 = Integer.parseInt(parts[2]);
    String operator = parts[1];

    switch (operator) {
        case "+":
            return num1 + num2;
        case "-":
            return num1 - num2;
        case "*":
            return num1 * num2;
        default:
            return num1 / num2;
    }
}

}

public class sice {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
QuizFrame frame = new QuizFrame();
frame.pack();
frame.setVisible(true);
});
}
}`

标签:进阶,num2,int,四则运算,private,可视化,operator,problem,num1
From: https://www.cnblogs.com/zyh-828/p/18442552/2024-9-30

相关文章

  • Python函数进阶:让你的代码更优雅的秘密武器
    引言你是否曾在编写Python代码时感到困惑,尤其是在处理函数时?你并不孤单!根据一项调查,超过70%的开发者在学习Python时都曾遇到过函数的各种难题。想象一下,如果你能掌握一些高级用法,像装饰器、匿名函数、可变参数和关键字参数等,你的代码将会变得多么优雅和高效!在这篇文章中,我们将......
  • AWK进阶教程:精通match函数,让字符串搜索游刃有余!
    AWK中的match函数允许你在字符串中搜索模式。在本教程中,你将学习如何使用awkmatch函数,基于匹配结果执行条件处理,并遍历字符串中的多个匹配项。语法和用法awkmatch函数的基本语法是:awk'{if(match($0,pattern))print$0;}'filename这里,$0表示整行输入,pat......
  • 【python进阶攻略10】异常、lambda表达式
    异常异常处理是一种艺术,一旦你掌握,会授予你无穷的力量。我将要向你展示我们能处理异常的一些方式。最基本的术语里我们知道了try/except从句。可能触发异常产生的代码会放到try语句块里,而处理异常的代码会在except语句块里实现。这是一个简单的例子:try:file=open(......
  • 【python进阶攻略11】一行式、For - Else
    一行式本章节,我将向大家展示一些一行式的Python命令,这些程序将对你非常有帮助。简易WebServer你是否想过通过网络快速共享文件?好消息,Python为你提供了这样的功能。进入到你要共享文件的目录下并在命令行中运行下面的代码:#Python2python-mSimpleHTTPServe......
  • 基于Python可视化的学习系统的设计与实现(源码+文档+调试+答疑)
    文章目录一、项目介绍二、视频展示三、开发环境四、系统展示五、代码展示六、项目文档展示七、项目总结大家可以帮忙点赞、收藏、关注、评论啦......
  • 【C++篇】领略模板编程的进阶之美:参数巧思与编译的智慧
    文章目录C++模板进阶编程前言第一章:非类型模板参数1.1什么是非类型模板参数?1.1.1非类型模板参数的定义1.2非类型模板参数的注意事项1.3非类型模板参数的使用场景示例:静态数组的实现第二章:模板的特化2.1什么是模板特化?2.1.1模板特化的分类2.2函数模板特化......
  • 随机三十道四则运算
    importjavax.swing.;importjava.awt.;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.Random;publicclassArithmeticTestGUIextendsJFrame{privatestaticfinalintTIME_LIMIT_SECONDS=360;privatestaticfina......
  • django基于Hadoop的气象数据的研究与应用(源码+vue+可视化大屏展示+爬虫分析+讲解等)
    收藏关注不迷路!!......
  • js进阶——FormData常用知识点介绍
    FormData是JavaScript中用于构建表单数据对象的API,它主要用于处理enctype="multipart/form-data"类型的表单提交,即上传文件和数据。通过FormData,开发者可以在客户端构建和发送表单数据,尤其是在没有使用传统的HTML表单提交时,允许开发者进行更多的自定义和控制。For......
  • springboot+vue招聘信息数据分析与可视化平台【开题+程序+论文】
    系统程序文件列表开题报告内容研究背景在当今数字化和信息化的时代背景下,招聘行业正经历着前所未有的变革。随着互联网技术的飞速发展,招聘信息的发布和获取方式日益多样化,但同时也带来了信息过载和筛选困难的问题。企业HR和求职者需要在海量的招聘信息中快速定位到符合自身......