2024.10.8(週二)

记得关月亮發表於2024-10-15
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;
        }
    }
}

生成算式並以不同方式儲存