首页 > 其他分享 >2024.10.8(生成算数)

2024.10.8(生成算数)

时间:2024-10-12 09:26:23浏览次数:1  
标签:2024.10 String num2 int question private 生成 算数 new

import javax.swing.;
import java.awt.
;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;

public class MathQuizApp extends JFrame {
private static final int QUIZ_TIME = 3 * 60; // 3
private static final int QUESTION_COUNT = 40; // Ŀ
private Random rand = new Random();
private HashMap<String, Integer> questions = new HashMap<>();
private JTextField[] answerFields;
private Timer timer;
private JLabel timerLabel;
private int remainingTime;

public MathQuizApp() {
    setTitle("Math Quiz");
    setLayout(new GridLayout(QUESTION_COUNT + 2, 1)); 
    setSize(400, 800);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    timerLabel = new JLabel("Time left: 3:00", SwingConstants.CENTER);
    timerLabel.setFont(new Font("Arial", Font.BOLD, 30));
    add(timerLabel);
    answerFields = new JTextField[QUESTION_COUNT];

    generateQuestions();
    displayQuestions();

    JButton submitButton = new JButton("Submit");
    submitButton.addActionListener(e -> checkAnswers());
    add(submitButton);

    startQuiz();
    setVisible(true);
}

private void startQuiz() {
    remainingTime = QUIZ_TIME;
    timer = new Timer(1000, e -> {
        remainingTime--;
        timerLabel.setText("    ʱ: " + formatTime(remainingTime));
        if (remainingTime <= 0) endQuiz();
    });
    timer.start();
}

private void generateQuestions() {
    HashSet<String> generatedQuestions = new HashSet<>();

    while (generatedQuestions.size() < QUESTION_COUNT) {
        int num1 = rand.nextInt(51);
        int num2 = rand.nextInt(51);
        char op = "+-*/".charAt(rand.nextInt(4));
        String question = "";

        switch (op) {
            case '+':
                question = String.format("%d + %d =", num1, num2);
                if (generatedQuestions.add(question))
                    questions.put(question, num1 + num2);
                break;
            case '-':
                if (num1 >= num2) {
                    question = String.format("%d - %d =", num1, num2);
                    if (generatedQuestions.add(question))
                        questions.put(question, num1 - num2);
                }
                break;
            case '*':
                if (num1 * num2 < 1000) {
                    question = String.format("%d * %d =", num1, num2);
                    if (generatedQuestions.add(question))
                        questions.put(question, num1 * num2);
                }
                break;
            case '/':
                if (num2 != 0 && (num1 % num2 == 0)) {
                    question = String.format("%d / %d =", num1, num2);
                    if (generatedQuestions.add(question))
                        questions.put(question, num1 / num2);
                }
                break;
        }
    }
}

private void displayQuestions() {
    for (int i = 0; i < QUESTION_COUNT; i++) {
        String question = (String) questions.keySet().toArray()[i];
        answerFields[i] = new JTextField(10);
        answerFields[i].setPreferredSize(new Dimension(60, 25));
        answerFields[i].setHorizontalAlignment(JTextField.CENTER);

        JPanel linePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        linePanel.add(new JLabel(question + " "));
        linePanel.add(answerFields[i]);

        add(linePanel);
    }
}

private void checkAnswers() {
    int score = 0;
    int wrongCount = 0;

    for (int i = 0; i < QUESTION_COUNT; i++) {
        String question = (String) questions.keySet().toArray()[i];
        int correctAnswer = questions.get(question);

        int userAnswer;
        try {
            userAnswer = Integer.parseInt(answerFields[i].getText());
        } catch (NumberFormatException e) {
            userAnswer = Integer.MIN_VALUE;
        }

        if (userAnswer == correctAnswer) {
            score++;
        } else {
            wrongCount++;
        }
    }

    double accuracy = (double) score / QUESTION_COUNT * 100;
    String resultMessage = String.format("Score: %d/%d\nWrong Count: %d\nAccuracy: %.2f%%",
            score, QUESTION_COUNT, wrongCount, accuracy);
    JOptionPane.showMessageDialog(this, resultMessage);
    System.exit(0);
}

private void endQuiz() {
    timer.stop();
    JOptionPane.showMessageDialog(this, "Time's up! Please submit your answers.");
    checkAnswers();
}

private String formatTime(int seconds) {
    int minutes = seconds / 60;
    int secs = seconds % 60;
    return String.format("%02d:%02d", minutes, secs);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(MathQuizApp::new);
}

}

标签:2024.10,String,num2,int,question,private,生成,算数,new
From: https://www.cnblogs.com/yangsongduo/p/18459808

相关文章

  • 2024.10.10
    Static当方法中不涉及到任何和对象相关的成员,则可以将方法设计成静态方法,提高开发效率,如:Math.sqrt()静态方法,只能访问静态的成员,非静态的方法,可以访问静态成员和非静态成员(必须遵守访问权限)注意这个的意思是静态方法不可以使用this访问本类的成员,但可以在静态方法内创建本......
  • 2024.10.7(数据结构的栈)
    顺序栈是利用顺序存储结构实现的栈,指针top指示栈顶在顺序栈的位置。base为存储空间基地址,S.top-S.base是栈中元素的个数,类似Length。栈为空时:S.topS.base;栈满时:S.top-S.baseMAXSIZE;顺序栈,top在最高元素的上一个,base位置是最低元素,故取栈顶元素要取top-1的:队列先进先出。......
  • 文件管理方案参考 2024.10.12
    文件管理方案参考2024.10.12说明:此文档中的文件是指手机、平板电脑、笔记本电脑等电子设备在使用过程中新建、接收、重命名、移动、编辑的电子文件。例如:Word文档(.docx)、Excel表格(.xlsx)、Photoshop图片(.jpg)、酷我音乐盒无损音乐歌曲(.flac)、国语中字电影视频(.MP4)、视频教程(.AVI)。......
  • python学习第二天(2024.10.11)
    python下载官网https://www.python.org/点击Downloads选择安装版本,选择操作系统选择操作系统位数,选择下载文件格式(ZIP、exe)下载安装(安装路径不能有中文和空格)验证是否安装成功win+r,输入cmd,直接输入python,显示正确安装版本及安装成功环境变量配置......
  • visual studio中的编译和build ,以及生成解决方案的区别
    在VisualStudio中,编译(Compile)、Build以及生成解决方案(BuildSolution)是软件开发过程中的重要环节,它们各自承担着不同的任务,但又相互关联。以下是这三者的区别和联系:一、编译(Compile)编译是将源代码转换为机器可读代码的过程。具体来说,编译程序会分析源代码,进行词法分析、语法分......
  • 最新Qt6将可执行文件打包为独立exe保姆级教学!含报错:无法定位程序输入点于动态链接库解
     相信大家都有类似的体验,自己已经在Qt练习中写出了不错的小程序,每次想发给别人体验都要发一整个大代码包,还得对面有对应的装好的QT才能运行,或者是想把自己的成果记录下来作为一个单独的exe文件却没有办法,今天教大家Qt如何生成独立可执行exe。注意:以下是Qt6之前版本可用的全过......
  • Java使用idea自动生成CRUD代码
    要在IntelliJIDEA中自动生成CRUD代码,你可以使用Lombok插件和MyBatis-Plus插件。以下是具体步骤:首先,确保你已经安装了IntelliJIDEA。如果没有,请从官网下载并安装:https://www.jetbrains.com/idea/download/打开IntelliJIDEA,点击菜单栏的File>Settings(或者使用快捷键Ctrl+Alt+S),在......
  • 生成系统签名.jsk
    生成系统签名1、在服务器内创建文件夹key用于放置签名文件2、生成platform.pemopensslpkcs8-informDER-nocrypt-inplatform.pk8-outplatform.pem3、生成pkcs12opensslpkcs12-export-inplatform.x509.pem-outplatform.p12-inkeyplatform.pem-passwordpas......
  • FLUX.1-dev-LoRA 混合现实与插画风格模型,生成你的卡通旅游照片
     FLUX.1-dev-LoRA是一个混合现实与插画风格模型。该模型可以生成融合了现实与插画元素的图像。它基于FLUX.1-dev基础模型进行训练,特别擅长生成前景角色为插画风格,而背景为现实风格的视觉效果。可以通过使用该模型生成图片并发布在社交平台上,如微信朋友圈或小红书,达到不露脸但......
  • 2024.10.11总结
    本文于github博客同步更新最简单但挂分最惨的一集。唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了唐死我了......