10月14日记录

pygmy-killer-whale / 2024-10-15 / 原文

java编程实现四则运算;
要求:1.区分二年级三年级,二年级两个100内操作数,三年级不超过四个1000内操作数;随机加减乘除
2.实现计数,判断正误

点击查看代码
import java.util.*;
abstract class MathProblem {
    protected int maxOperands; // 最大操作数
    protected boolean allowMultiplication; // 是否允许乘法
    protected boolean allowDivision; // 是否允许除法
    protected boolean allowParentheses; // 是否允许括号(四年级)
    protected int maxRange; // 数值范围
    protected Random random = new Random();
    public MathProblem(int maxOperands, boolean allowMultiplication, boolean allowDivision, boolean allowParentheses, int maxRange) {
        this.maxOperands = maxOperands;
        this.allowMultiplication = allowMultiplication;
        this.allowDivision = allowDivision;
        this.allowParentheses = allowParentheses;
        this.maxRange = maxRange;
    }
    public abstract String generateProblem();
    public abstract double evaluate(String expression);
    public abstract boolean checkAnswer(String userAnswer, double correctAnswer);
}
class SecondGradeProblem extends MathProblem {
    public SecondGradeProblem() {
        super(2, true, true, false, 100);
    }
    public String generateProblem() {
        int a = random.nextInt(maxRange + 1);
        int b = random.nextInt(maxRange + 1);
        String operator = getRandomOperator();
        // 确保除法整除
        if (operator.equals("/")) {
            if (b == 0) b = 1; // 防止除以0
            a -= a % b; // 确保整除
        }
        return a + " " + operator + " " + b;
    }
    public double evaluate(String expression) {
        String[] tokens = expression.split(" ");
        int a = Integer.parseInt(tokens[0]);
        String operator = tokens[1];
        int b = Integer.parseInt(tokens[2]);
        switch (operator) {
            case "+":
                return a + b;
            case "-":
                return a - b;
            case "*":
                return a * b;
            case "/":
                return b != 0 ? a / b : 0;
        }
    }
    public boolean checkAnswer(String userAnswer, double correctAnswer) {
        return String.valueOf((int) correctAnswer).equals(userAnswer);
    }
    public String getRandomOperator() {
        List<String> operators = new ArrayList<>(Arrays.asList("+", "-", "*", "/"));
        return operators.get(random.nextInt(operators.size()));
    }
}
class ThirdGradeProblem extends SecondGradeProblem {
    public ThirdGradeProblem() {
        super();
        this.maxOperands = 4; // 操作数最多4个
    }
    public String generateProblem() {
        StringBuilder problem = new StringBuilder();
        int operandCount = random.nextInt(maxOperands - 1) + 2; // 生成2到4个操作数
        for (int i = 0; i < operandCount; i++) {
            int num = random.nextInt(maxRange + 1);
            problem.append(num);
            if (i < operandCount - 1) {
                problem.append(" ").append(getRandomOperator()).append(" ");
            }
        }
        return problem.toString();
    }
}
public class MathQuiz {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        WrongProblemManager wrongProblemManager = new WrongProblemManager();
        while (true) {
            System.out.println("请选择年级(2、3、4 或 0退出):");
            int grade = scanner.nextInt();
            if (grade == 0) break;
            System.out.print("输入题目数量:");
            int numberOfProblems = scanner.nextInt();
            MathProblem problem = null;
            switch (grade) {
                case 2:
                    problem = new SecondGradeProblem();
                    break;
                case 3:
                    problem = new ThirdGradeProblem();
                    break;
                case 4:
                    problem = new FourthGradeProblem();
                    break;
            }

            for (int i = 0; i < numberOfProblems; i++) {
                String question = problem.generateProblem();
                System.out.println("问题 " + (i + 1) + ": " + question);
                System.out.print("请输入答案: ");
                String userAnswer = scanner.next();
                try {
                    double correctAnswer = problem.evaluate(question);
                    if (!problem.checkAnswer(userAnswer, correctAnswer)) {
                        System.out.println("回答错误!");  
                    } else {
                        System.out.println("回答正确!");
                    }
                } catch (Exception e) {
                    System.out.println("计算过程中出错: " + e.getMessage());
                }
            }

            System.out.print("是否查看错题本?(Y/N): ");
            if (scanner.next().equalsIgnoreCase("Y")) {
                wrongProblemManager.displayWrongProblems();
            }

            System.out.print("是否开始下一套题目?(Y/N): ");
            if (!scanner.next().equalsIgnoreCase("Y")) {
                break;
            }
        }
        scanner.close();
    }
}