首页 > 其他分享 >2024.10.8(周二)

2024.10.8(周二)

时间:2024-10-15 16:43:35浏览次数:6  
标签:2024.10 String num2 int private 周二 new problem

import java.io.*;
import java.util.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class MathQuizGenerator {
    private final List<MathProblem> problems = new ArrayList<>();
    private final Random random = new Random();

    public static void main(String[] args) throws Exception {
        MathQuizGenerator generator = new MathQuizGenerator();
        generator.generateProblems(10); // 生成10个算式
        generator.saveToCSV("math_problems.csv");
        generator.saveToXML("math_problems.xml");
        generator.saveToJSON("math_problems.json");
        
        generator.gradeProblems();
    }

    private void generateProblems(int count) {
        for (int i = 0; i < count; i++) {
            int num1 = random.nextInt(101); // 0到100
            int num2 = random.nextInt(101);
            String operator = getRandomOperator();
            String expression = num1 + " " + operator + " " + num2;
            int answer = calculate(num1, num2, operator);
            problems.add(new MathProblem(expression, answer));
        }
    }

    private String getRandomOperator() {
        String[] operators = {"+", "-", "*", "/"};
        return operators[random.nextInt(operators.length)];
    }

    private int calculate(int num1, int num2, String operator) {
        switch (operator) {
            case "+": return num1 + num2;
            case "-": return num1 - num2;
            case "*": return num1 * num2;
            case "/": return num2 != 0 ? num1 / num2 : num1; // 防止除以0
            default: return 0;
        }
    }

    private void saveToCSV(String filename) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
            writer.write("Problem,Answer\n");
            for (MathProblem problem : problems) {
                writer.write(problem.getExpression() + "," + problem.getAnswer() + "\n");
            }
        }
    }

    private void saveToXML(String filename) throws Exception {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("MathProblems");
        doc.appendChild(rootElement);

        for (MathProblem problem : problems) {
            Element problemElement = doc.createElement("Problem");
            problemElement.setAttribute("expression", problem.getExpression());
            problemElement.setAttribute("answer", String.valueOf(problem.getAnswer()));
            rootElement.appendChild(problemElement);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filename));
        transformer.transform(source, result);
    }

    private void saveToJSON(String filename) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(new File(filename), problems);
    }

    private void gradeProblems() {
        Scanner scanner = new Scanner(System.in);
        for (MathProblem problem : problems) {
            System.out.print("Solve: " + problem.getExpression() + " = ");
            int userAnswer = scanner.nextInt();
            System.out.println(userAnswer == problem.getAnswer() ? "Correct!" : "Wrong! The correct answer is " + problem.getAnswer());
        }
        scanner.close();
    }

    static class MathProblem {
        private final String expression;
        private final int answer;

        public MathProblem(String expression, int answer) {
            this.expression = expression;
            this.answer = answer;
        }

        public String getExpression() {
            return expression;
        }

        public int getAnswer() {
            return answer;
        }
    }
}

生成算式并以不同方式存储

标签:2024.10,String,num2,int,private,周二,new,problem
From: https://www.cnblogs.com/Sunyiran/p/18467864

相关文章

  • 2024.10.15人工智能学记3
    老师先讲了AI的定义:人工智能(AI)是计算机科学的一个分支,致力于创造能够模仿人类智能行为的机器或系统。这与教育学中的"智能”概念有些相似,但范围更广,包括感知、学习、推理、问题解决等能力。以及如何从教育者角度来理解AI?①规则基础系统-教学大纲和课程设置;机器学习-学生通过练......
  • 2024.10.15 1132版
    起于《海奥华预言》的思考◆地球管理结构和参考持续更新中...... 英文地址:https://github.com/zhuyongzhe/Earth/tags中文地址:https://www.cnblogs.com/zhuyongzhe85作者:朱永哲 ---------------------------------------------------------------------------------......
  • 2024.10.14
    刷新页面后,Vuex中的数据被重置的原因是Vuex状态存储在内存中,当页面刷新时,整个JavaScript运行环境会重新加载,Vuex中的数据也会丢失。因此,this.$store.state.user.userId在页面刷新后可能会变成null或undefined。要解决这个问题,你可以将用户数据(如userId)持久化到浏览器......
  • 2024.10 记录(1)
    \(2024\)年\(10\)月记录qwq。20240929联考T3考虑怎么快速判定一个图是否有四元环,可以度数小定向连到度数大的点,由三元环计数的分析方法,枚举\(i\)相邻的\(u\),定向连出的\(v\),判定\(i,v\)是否有向相邻,是\(O(m\sqrtm)\)的。考虑\(u\)的度数为\(d_u\),现在来分析一......
  • 2024.10.13 模拟赛
    2024.10.13模拟赛T1「KDOI-10」商店砍价赛时直接口胡出一个错误的贪心。一开始的想法是按照\(v[i]\)排序,然后假设输入的长度为\(n\)位。那么对于排序后\(n-5\)位直接选择操作一。对于剩下的,直接bdfs所有情况选答案,复杂度\(O(n\logn)\)。貌似可行,结果随便一个数据......
  • 2024.10.14 test
    B平面上有\(n\)个点以及\(k\)条未知的平行线,每个点都分属一条线,每条线都有至少\(2\)点。给出一种方案。\(n\le4e4,k\le50\)。每个点分属一条线的条件非常重要。考虑利用鸽巢原理。考虑取出\(k+1\)个没有两对点同斜率的点,那么,至少有两个点在一条线上,那么就可以确定斜......
  • 云原生周刊:优化 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作者:朱永哲 ---------------------------------------------------------------------------------......
  • 2024.10.14 1005版
    起于《海奥华预言》的思考◆地球管理结构和参考持续更新中...... 英文地址:https://github.com/zhuyongzhe/Earth/tags中文地址:https://www.cnblogs.com/zhuyongzhe85作者:朱永哲 ---------------------------------------------------------------------------------......