首页 > 其他分享 >10月15日

10月15日

时间:2024-10-15 20:32:05浏览次数:6  
标签:10 15 String int System problem public out

优化上次的四则运算代码;
增加要求:1.添加四年级能够进行五位操作数以内运算;

点击查看代码
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);
    }

    @Override
    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;
    }

    @Override
    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;
            default:
                throw new UnsupportedOperationException("不支持的操作符: " + operator);
        }
    }

    @Override
    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个
    }

    @Override
    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();
    }
}

class FourthGradeProblem extends ThirdGradeProblem {
    public FourthGradeProblem() {
        super();
        this.maxOperands = 5; // 操作数最多5个
    }

    @Override
    public String generateProblem() {
        StringBuilder problem = new StringBuilder();
        int operandCount = random.nextInt(maxOperands - 1) + 2; // 生成2到5个操作数

        boolean useParentheses = random.nextBoolean() && operandCount > 2 && allowParentheses;
        return problem.toString();
    }
}

// 错题管理类
class WrongProblemManager {
    private final Map<String, Integer> wrongProblems = new HashMap<>();

    public void addWrongProblem(String problem,double answer) {
        wrongProblems.put(problem, wrongProblems.getOrDefault(problem, 0) + 1);
        double correctanswer[]={0};
        correctanswer[0]=answer;
    }

    public void displayWrongProblems() {
        if (wrongProblems.isEmpty()) {
            System.out.println("没有错题记录。");
        } else {
            System.out.println("错题集:");
            wrongProblems.forEach((problem, count) -> System.out.println(problem + " - 错误次数:" + count));
        }
    }
    public void redoWrongProblems() {
        Scanner scanner = new Scanner(System.in);
        MathProblem problemmath = null;
        WrongProblemManager wrongProblemManager = new WrongProblemManager();
        wrongProblems.forEach((problem, count) -> System.out.println(problem));
        System.out.println("请输入答案:");
        String anwser = scanner.next();
        try {
            double correctAnswer = problemmath.evaluate(problemmath.generateProblem());
            if (!problemmath.checkAnswer(anwser, correctAnswer)) {
                System.out.println("回答错误!");
            } else {
                System.out.println("回答正确!");
            }
        } catch (Exception e) {
            System.out.println("计算过程中出错: " + e.getMessage());
        }
    }
}

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;
                default:
                    System.out.println("无效年级,请重新选择!");
                    continue;
            }
            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("回答错误!");
                        wrongProblemManager.addWrongProblem(question, correctAnswer);
                    } 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.println("是否进行错题练习?(Y/N):");
            if (scanner.next().equalsIgnoreCase("Y")) {

            }
            System.out.print("是否开始下一套题目?(Y/N): ");
            if (!scanner.next().equalsIgnoreCase("Y")) {
                break;
            }
        }
        scanner.close();
    }
    private static void domathnumber(int numberOfProblems, String problem,){

    }
}
上午完成了工程实训制作小铁盒; 下午学习了统一建模语言系统实例结构图;

标签:10,15,String,int,System,problem,public,out
From: https://www.cnblogs.com/pygmy-killer-whale/p/18468386

相关文章

  • 第五周(10.8-
    代码题:1、给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。题解:如果等于nums[middle],返回middle;否则返回left或者low。2、在排序数组中查找target的开始位置和结束位置。二分法不可能会漏掉正确结果的......
  • 【PAT_Python解 带全部测试点】1068 万绿丛中一点红
    原题链接:PTA|程序设计类实验辅助教学平台测试点0:独一无二测试点1: NotUnique测试点2:NotExist测试点3:独一无二       点在边缘(相邻数值不足8个)45207000000000400000070答案:(4,3):4测试点5:点在边缘(相邻数值不足8个)4520000......
  • 【人工智能/计算机工程/大数据】第五届人工智能与计算工程国际学术会议(ICAICE 2024,202
    The5thInternationalConferenceonArtificialIntelligenceandComputerEngineering第五届人工智能与计算工程国际学术会议(ICAICE2024)会议官网:www.event-icaice.orgThe5thInternationalConferenceonArtificialIntelligenceandComputerEngineer......
  • 【SPIE独立出版】2024年第三届信息学,网络与计算技术国际学术会议(ICINC2024,10月25-27)
    2024年第三届信息学,网络与计算技术国际学术会议(ICINC2024)将于2024年10月25-27日于中国郑州召开。会议将围绕信息技术与通信,网络与计算技术等在相关领域中的最新研究成果,为来自国内外高等院校、科学研究所、企事业单位的专家、教授、学者、工程师等提供一个分享专业经验......
  • 2024.10.15 比赛反思
    2024.10.15比赛反思其实我觉得有好几次我差一点就要写反思了,但是由于运气最后没有写(最极限的一次是倒数第五)。但是终究还是逃不过啊。首先是\(T1\)还比较正常,用了大概\(50\min\),没有浪费太多时间,这点比较好。但是后面就开始出现问题了。\(T2\)是一个网格图上的问题,其实感......
  • dll修复工具c2015更新失败怎么办?dll修复工具c2015更新失败详细解决步骤
    针对“dll修复工具c2015更新失败”的问题,这里提供一系列可能的解决方案。这些方案旨在帮助用户解决在尝试更新或修复VC2015相关的DLL文件时遇到的失败情况。请注意,以下步骤可能需要根据具体情况进行调整:一、检查系统更新确保系统最新:首先,确保Windows系统已安装所有可用的更......
  • 【2024.10.14(?) 闲话】飞升
    今日推歌:神曲-RSoundDesign出题人怎么这么没素质。暴力哥获得了320分!而我t4暴力的bitset只开了30000,t2没冲出来,输麻了!我要飞升了!(注:起死回生,飞升上天)(注:某人的rating飞升记录)到底是谁在剪辑这样的视频。但是我要飞升!!!!11其实这是昨天的闲话。但是昨天忘记发了......
  • 总结 2024.10.15
    放链接考试总结忘记了pwp后面一定都写ATDPcontest近期主线,在补dpabc370f题解思维题,暂时没怎么写有点鸽,补完dp再补晚自习晚自习again组合数学训练......
  • 9-10上月总结
    考试总结test20240907test20240914test20240915test20241006有点唐氏,十月只写了一次总结,主要是我认为题目有点奇异。贪心构造专题数学专题学习笔记(可持久化)权值线段树树链剖分|树上启发式合并刷题trichlorotrifluoroethane(CCF题)板板刷花(CF、AT)......
  • 24.10.15
    谁家好人往NOIp模拟赛里塞CF*3500啊。A考察\(x\)与\(<x\)的点的连边。\[\begin{aligned}&x|(y+n)\\&kx=y+n\\&y=kx-n\\&\because0<y<x\len\\&\thereforey只有1个\\\Rightarrow&k=\left\lceil\frac{......