首页 > 其他分享 >10月14日记录

10月14日记录

时间:2024-10-14 19:26:41浏览次数:7  
标签:10 String 记录 int System problem out public 14

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

标签:10,String,记录,int,System,problem,out,public,14
From: https://www.cnblogs.com/pygmy-killer-whale/p/18464813

相关文章

  • 2023 ThinkPad运行Linux选购思考记录
    2023ThinkPad运行Linux选购思考记录TieraRedHat高级混合云架构师/RHCALevel3​关注 17人赞同了该文章​展开目录 前言在当今这个数字化时代,笔记本电脑已成为我们日常生活和工作中不可或缺的工具。对于软件工程师和系......
  • 2024/10/13 模拟赛总结
    人机体检,\(0+0+0+0=0\),打代码源去了#A.一般图最小匹配下次看到这种范围一定要想到dp啊,令\(dp_{i,j}\)为前\(i\)个元素选了\(j\)对点的最小代价由于边权是绝对值,可以对原数组排一遍序,选取的两个点就一定在排序后数组的相邻节点那么就可以得出式子:\(dp_{i,j}=\min\{dp_......
  • 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.全程离线精简、无人值守调用优化处理制作。部分优化适配系统可能要重启几次,即使显示适配失败也不要在意,可能......
  • HTB打靶记录-Cicada
    NmapScannmap扫描一下ipnmap-sT-sV-O-Pn10.10.11.35Nmapscanreportfor10.10.11.35Hostisup(0.012slatency).Notshown:989filteredtcpports(no-response),1filteredtcpports(host-unreach)PORTSTATESERVICEVERSION25/tcpopensmtp?......
  • 题解:P11145 Strange Homura Game
    ProblemLinkStrangeHomuraGame题意让你猜测一个数\(n\),你只能输出两次,每次输出一个数\(x\),返回\(x\bmodn\)。Solution令输入的数为\(A,B\),输出的数为\(a,b\),答案为\(n\)。一开始想的是CRT,但只能询问\(2\)次。发现输入的值是经过\(\bmodn\)的,已知\((A-a)......
  • 题解:P11063 【MX-X4-T3】「Jason-1」数对变换
    ProblemLink【MX-X4-T3】「Jason-1」数对变换题外话场上把贪心注重在一个奇怪地方了,导致交的时候已经有\(9\)个人\(\textcolor{green}{AC}\)了(哭)。题意简述对于数对\((x,y)\),你可以执行以下两种变换:类型1:取\(1\lek\lex\),令\((x,y)\gets(\lfloor\frac{x}{k}......
  • 题解:P10370 「LAOI-4」Mex Tower (Hard ver.)
    ProblemLink「LAOI-4」MexTower(Hardver.)题意给定一个长度为$n$的序列$a$,求序列的$\operatorname{Mex}$值是否大于等于其他所有长度为$n$的自然数序列的$\operatorname{Mex}$值。Solution不难发现,两个数或一个序列的$\operatorname{Mex}$一定是......
  • P6148 [USACO20FEB] Swapity Swapity Swap S
    ​P6148[USACO20FEB]SwapitySwapitySwapSFarmerJohn的\(N\)头奶牛(\(1\leqN\leq10^5\))站成一排。对于每一个\(1\leqi\leqN\),从左往右数第\(i\)头奶牛的编号为\(i\)。FarmerJohn想到了一个新的奶牛晨练方案。他给奶牛们\(M\)对整数\((L_1,R_1)\ldots(L_M,......
  • 视频如何压缩到100m以下?5种方法教你快速压缩
    视频已成为我们生活中不可或缺的一部分。然而,高清视频往往伴随着庞大的文件体积,给存储和分享带来了诸多不便,这时就需要对视频进行适当的压缩,以达到我们的需求。下面来给大家分享5种超简单的视频压缩方法,不会损坏画质,一起来学习下吧。方法一:迅捷视频转换器这是一款功能强大的......