首页 > 其他分享 >10.14

10.14

时间:2024-10-14 23:44:36浏览次数:5  
标签:String int private add new questionCount 10.14

实现了课上部分要求
QixunluGUI类

点击查看代码
package qixun;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;


public class QixunluGUI extends JFrame {
    private static final int TIME_LIMIT = 60;
    private int questionCount;
    private String[] questions;
    private double[] correctAnswers;
    private JTextField[] answerFields;
    private JLabel[] resultLabels;
    private JLabel timerLabel = new JLabel("剩余时间: " + TIME_LIMIT + "秒");
    private int correctCount = 0;
    private int timeRemaining = TIME_LIMIT;

    public QixunluGUI(int questionCount, int gradeLevel) {
        this.questionCount = questionCount;
        this.questions = new String[questionCount];
        this.correctAnswers = new double[questionCount];
        this.answerFields = new JTextField[questionCount];
        this.resultLabels = new JLabel[questionCount];

        setTitle("数学测验");
        setSize(500, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 生成问题
        QuestionGenerator generator = new QuestionGenerator(gradeLevel);
        for (int i = 0; i < questionCount; i++) {
            questions[i] = generator.getQuestion();
            correctAnswers[i] = generator.getAnswer(questions[i]);
        }

        // 创建一个面板用于问题布局,三道题一行
        JPanel questionPanel = new JPanel(new GridLayout((questionCount + 2) / 3, 4));
        for (int i = 0; i < questionCount; i++) {
            questionPanel.add(new JLabel("问题 " + (i + 1) + ": " + questions[i]));
            answerFields[i] = new JTextField(10);
            questionPanel.add(answerFields[i]);
            resultLabels[i] = new JLabel();
            questionPanel.add(resultLabels[i]);

            JButton checkButton = new JButton("提交");
            int finalI = i;
            checkButton.addActionListener(e -> submitSingleAnswer(finalI, resultLabels[finalI]));
            questionPanel.add(checkButton);
        }

        // 创建一个面板用于放置总提交按钮
        JPanel submitPanel = new JPanel(new FlowLayout());
        JButton finalSubmitButton = new JButton("提交所有答案");
        JLabel finalResultLabel = new JLabel();

        // 处理倒计时
        Timer timer = new Timer(1000, e -> {
            timeRemaining--;
            timerLabel.setText("剩余时间: " + timeRemaining + "秒");
            if (timeRemaining <= 0) {
                ((Timer) e.getSource()).stop();
                JOptionPane.showMessageDialog(null, "时间到!");
                submitAllAnswers(finalResultLabel);
            }
        });
        timer.start();

        // 设置总提交按钮事件
        finalSubmitButton.addActionListener(e -> {
            timer.stop();
            submitAllAnswers(finalResultLabel);
        });

        // 将提交按钮和结果标签加入提交面板
        submitPanel.add(finalSubmitButton);
        submitPanel.add(finalResultLabel);

        add(timerLabel, BorderLayout.NORTH);
        add(new JScrollPane(questionPanel), BorderLayout.CENTER); // 添加问题面板
        add(submitPanel, BorderLayout.SOUTH); // 提交按钮单独一行

        setVisible(true);
    }

    private void submitSingleAnswer(int index, JLabel resultLabel) {
        try {
            double userAnswer = Double.parseDouble(answerFields[index].getText());
            if (Math.abs(userAnswer - correctAnswers[index]) < 0.001) {
                correctCount++;
                resultLabel.setText("正确");
            } else {
                resultLabel.setText("错误,正确答案是: " + correctAnswers[index]);
            }
        } catch (NumberFormatException ex) {
            resultLabel.setText("请输入有效的数字");
        }
    }

    private void submitAllAnswers(JLabel finalResultLabel) {
        finalResultLabel.setText("总正确率: " + (correctCount / (double) questionCount * 100) + "%");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入题目数量: ");
        int questionCount = scanner.nextInt();
        System.out.print("请输入年级(2-4): ");
        int gradeLevel = scanner.nextInt();
        new QixunluGUI(questionCount, gradeLevel);
    }
}
WrongQuestionManager类
点击查看代码
package qixun;

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;


public class WrongQuestionManager {
    private Map<String, Integer> wrongQuestions = new HashMap<>();

    public void saveWrongQuestion(String question, String correctAnswer) {
        wrongQuestions.put(question, wrongQuestions.getOrDefault(question, 0) + 1);
        // 将错题保存到文件
    }

    public void retryWrongQuestions() {
        // 进行错题重练逻辑
    }
}

QuestionGenerator类 package qixun;

import java.util.;
import javax.script.
;

public class QuestionGenerator {
private int gradeLevel;
private int operandCount;
private boolean includeMulDiv;
private boolean includeBrackets;
private int range;
private Random random;

public QuestionGenerator(int gradeLevel) {
    this.gradeLevel = gradeLevel;
    this.random = new Random();
}

// 生成并返回一个数学题目(字符串)
public String getQuestion() {
    int num1 = random.nextInt(100); // 随机生成一个100以内的数字
    int num2 = random.nextInt(1, 101); // 随机生成另一个100以内的数字,确保不为0(用于除法)
    char operator = randomOperator();

    // 返回一个题目字符串
    return num1 + " " + operator + " " + num2;
}

// 根据生成的题目,计算并返回正确答案
public double getAnswer(String question) {
    // 从问题字符串中解析操作数和运算符
    String[] parts = question.split(" ");
    int num1 = Integer.parseInt(parts[0]);
    char operator = parts[1].charAt(0);
    int num2 = Integer.parseInt(parts[2]);

    switch (operator) {
        case '+':
            return num1 + num2;
        case '-':
            return num1 - num2;
        case '*':
            return num1 * num2;
        case '/':
            // 避免除以0
            return num2 != 0 ? num1 / (double) num2 : 0;
        default:
            throw new IllegalArgumentException("不支持的运算符: " + operator);
    }
}

// 随机生成一个运算符
private char randomOperator() {
    char[] operators = {'+', '-', '*', '/'};
    return operators[random.nextInt(operators.length)];
}
public QuestionGenerator(int gradeLevel, int operandCount, boolean includeMulDiv, boolean includeBrackets, int range) {
    this.gradeLevel = gradeLevel;
    this.operandCount = operandCount;
    this.includeMulDiv = includeMulDiv;
    this.includeBrackets = includeBrackets;
    this.range = range;
}

public String generateQuestion() {
    Random rand = new Random();
    List<Integer> operands = new ArrayList<>();
    List<String> operators = new ArrayList<>();

    for (int i = 0; i < operandCount; i++) {
        operands.add(rand.nextInt(range));
        if (i < operandCount - 1) {
            if (includeMulDiv) {
                operators.add(new String[]{"+", "-", "*", "/"}[rand.nextInt(4)]);
            } else {
                operators.add(new String[]{"+", "-"}[rand.nextInt(2)]);
            }
        }
    }

    if (includeBrackets) {
        // 随机插入括号逻辑
    }

    return buildExpression(operands, operators);
}

private String buildExpression(List<Integer> operands, List<String> operators) {
    // 构建表达式字符串
    StringBuilder expression = new StringBuilder();
    for (int i = 0; i < operands.size(); i++) {
        expression.append(operands.get(i));
        if (i < operators.size()) {
            expression.append(" ").append(operators.get(i)).append(" ");
        }
    }
    return expression.toString();
}

}

标签:String,int,private,add,new,questionCount,10.14
From: https://www.cnblogs.com/qiixunlu/p/18466491

相关文章

  • 2024.10.14
    刷新页面后,Vuex中的数据被重置的原因是Vuex状态存储在内存中,当页面刷新时,整个JavaScript运行环境会重新加载,Vuex中的数据也会丢失。因此,this.$store.state.user.userId在页面刷新后可能会变成null或undefined。要解决这个问题,你可以将用户数据(如userId)持久化到浏览器......
  • 10.14日
    基本的匿名函数可以直接定义一个匿名函数,然后立即调用它。javascript(function(){console.log("这是一个匿名函数!");})();2.作为回调函数匿名函数非常常见于回调场景,比如数组的方法。javascriptconstnumbers=[1,2,3,4,5];constdoubled=numbers.map(functi......
  • 10.14
    请根据课堂讲解,列举出口算题卡软件的功能列表描述,包括但不限于重复题目的检测、题目数字范围设置、加减乘除算式的参数化等扩展功能,鼓励参考其他成熟软件的功能进行设计,力求功能使用,可推广。直接在文本框提交文字即可,不要上传文档附件。我的答案: 基础功能:1.随机生成题目能......
  • 2024.10.14 test
    B平面上有\(n\)个点以及\(k\)条未知的平行线,每个点都分属一条线,每条线都有至少\(2\)点。给出一种方案。\(n\le4e4,k\le50\)。每个点分属一条线的条件非常重要。考虑利用鸽巢原理。考虑取出\(k+1\)个没有两对点同斜率的点,那么,至少有两个点在一条线上,那么就可以确定斜......
  • 【2024潇湘夜雨】WIN10_Ent-G_22H2.19045.5011软件选装纯净特别版10.14
    【系统简介】=============================================================1.本次更新母盘来自WIN10_Ent-G_22H2.19045.5011.进桌面后稍等片刻,等待后续部分优化完成。2.全程离线精简、无人值守调用优化处理制作。部分优化适配系统可能要重启几次,即使显示适配失败也不要在意,可能......
  • 10.14 ~ 10.20
    10.14上午模拟赛。但是这场模拟赛原先的题目叫“CSP-S模拟(难)”然后“题目不按照难度排序”而且还直接给了T4的初步结论有一种不祥的预感......
  • 10.14考试总结
    0+100+0,这也没啥好说的了,反正就差的一批吧……\(T1\)\(Hunter\)简单数论题,但\(lyh\)从来没有在考试的时候\(A\)过数论题。考虑第一个人挂的时间\(=\)其他人比第一个人早挂的概率。对于第\(i\)个人,简化问题,只留第一个人和第\(i\)个人,答案就是\(\dfrac{w_i}{w_1+w_......
  • 云原生周刊:优化 Uber 的持续部署丨2024.10.14
    开源项目推荐CogCog是将机器学习模型打包到容器的工具。可通过配置将机器学习模型所需的环境和依赖,自动打包到容器里方便部署,让你不再为编写Docker文件和CUDA而痛苦,还能自动启动HTTP接口服务方便调用。KnowStreamingKnowStreaming是功能强大的Kafka集群监控和运维管......
  • 2024.10.14 1105版
    起于《海奥华预言》的思考◆地球管理结构和参考持续更新中...... 英文地址:https://github.com/zhuyongzhe/Earth/tags中文地址:https://www.cnblogs.com/zhuyongzhe85作者:朱永哲 ---------------------------------------------------------------------------------......
  • 2024.10.14 1020版
    起于《海奥华预言》的思考◆地球管理结构和参考持续更新中...... 英文地址:https://github.com/zhuyongzhe/Earth/tags中文地址:https://www.cnblogs.com/zhuyongzhe85作者:朱永哲 ---------------------------------------------------------------------------------......