100以内加减混合

该代码段展示了一个Java程序,用于生成100以内的加减混合计算题目,每页包含20道题,总共有56页。题目类型包括加法、减法、乘法、除法以及括号表达式,确保了题目的多样性。程序将生成的题目写入到文本文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

随机生成100以内加减混合,每页20道共56页的计算题 

public static void main(String[] args) {
        /** 混合计算 */
        homework2();

    }

    public static void homework2() {

        Random random = new Random();
        StringBuffer resultAll = new StringBuffer();
        for (int j = 1; j < 57; j++) {
            StringBuffer res = new StringBuffer();
            for (int i = 1; i < 21; i++) {
                /** 0是加乘 1是加乘  2加减乘 3乘减 4加除 5除加 6减除 7除减 8 括号*/
                int strLeng = 0;
                int type = random.nextInt(9);
                String s = "";
                System.out.println(type+"===========");

                if (type == 0 || type == 1) {
                    s = getMultipliceAdd(type + "");
                    res.append(s);
                } else if (type == 2 || type == 3) {
                    s = getMultipliceReduce(type + "");
                    res.append(s);
                } else if (type == 4 || type == 5) {
                    s = getDivisionAdd(type + "");
                    res.append(s);
                } else if (type == 6 || type == 7) {
                    s = getDivisionRed(6 + "");
                    res.append(s);
                } else if (type == 8) {
                    s = bracket();
                    res.append(s);
                }
                strLeng = s.length();
                if (i > 0 && i % 3 == 0) {
                    res.append("\n");
                    res.append("\n");
                    res.append("\n");
                    res.append("\n");
                    res.append("\n");
                    res.append("\n");
                }else {
                    for (int k = 0; k < 30-strLeng; k++) {
                        res.append(" ");
                    }
                }
            }
            res.append("\n");
            res.append("\n");
            res.append("\n");
            res.append("\n");
            res.append("\n");
            res.append("\n");
//            res.append("                                 " + DateUtils.addDayStr(new Date(), j - 3) + "                ");
            res.append("                                 " + j + "                ");
            res.append("\n");
            res.append("\n");
            resultAll.append(res);
        }

        textToFile("E:\\暑假作业\\100以内加减混合.txt", resultAll.toString());
    }
    /** 括号 */
    public static String bracket(){
        Random random = new Random();
        int type = random.nextInt(4);
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < 500; i++) {
            int numC = random.nextInt(99);
            int numA = random.nextInt(99);
            int numB = random.nextInt(99);
            if (type == 0 && numA + numB + numC < 101){
                result.append(" ");
                result.append(numA);
                result.append(" + ");

                if (numB > numC){
                    result.append("(");
                    result.append(numB);
                    result.append(" - ");
                    result.append(numC);
                    result.append(")");
                }else{
                    result.append("(");
                    result.append(numB);
                    result.append(" + ");
                    result.append(numC);
                    result.append(")");
                }
                result.append(" = ");
                break;
            }else if (type == 1 && (numA + numB - numC) < 101&&(numA + numB - numC) >0){
                result.append(" ");
                result.append(numA);
                result.append(" + ");
                if (numB > numC){
                    result.append("(");
                }
                result.append(numB);
                result.append(" - ");
                result.append(numC);
                if (numB > numC){
                    result.append(")");
                }
                result.append(" = ");
                break;
            }else if (type == 2 && numA + numB - numC < 101&& numA + numB - numC >0){
                result.append(" ");
                result.append(numA);
                result.append(" + ");
                if (numB > numC){
                    result.append("(");
                }
                result.append(numB);
                result.append(" - ");
                result.append(numC);
                if (numB > numC){
                    result.append(")");
                }
                result.append(" = ");
                break;
            }else if (type == 3 && numC - numB - numA >0){
                result.append(" ");
                result.append(numC);
                result.append(" - ");
                result.append("(");
                result.append(numB);
                if (numB-numA >0){
                    result.append(" - ");
                }else{
                    result.append(" + ");
                }
                result.append(numA);
                result.append(")");
                result.append(" = ");
                break;
            }
        }
        return result.toString();

    }
    /**
     * 获取乘法减法算式
     */
    public static String getMultipliceReduce(String type) {
        StringBuffer result = new StringBuffer();
        Random random = new Random();
        for (int j = 0; j < 500; j++) {
            int numRed = random.nextInt(99);
            int numA = random.nextInt(9);
            int numB = random.nextInt(9);
            if (numA > 0 && numB > 0) {
                if ("2".equals(type) && numRed >= numA * numB) {
                    result.append(" ");
                    result.append(numRed);
                    result.append(" - ");
                    result.append(numA);
                    result.append(" x ");
                    result.append(numB);
                    result.append(" = ");
                    break;
                } else if ("3".equals(type) && numRed < numA * numB) {
                    result.append(" ");
                    result.append(numA);
                    result.append(" x ");
                    result.append(numB);
                    result.append(" - ");
                    result.append(numRed);
                    result.append(" = ");
                    break;
                }
            }
        }
        return result.toString();
    }

    /*乘法加法 */
    public static String getMultipliceAdd(String type) {
        StringBuffer result = new StringBuffer();
        Random random = new Random();
        for (int j = 0; j < 500; j++) {
            int numAdd = random.nextInt(99);
            int numA = random.nextInt(9);
            int numB = random.nextInt(9);
            if (numA > 0 && numB > 0 && (numA * numB + numAdd) < 101) {
                result.append(" ");
                if ("0".equals(type)) {
                    result.append(numAdd);
                    result.append(" + ");
                    result.append(numA);
                    result.append(" x ");
                    result.append(numB);
                    result.append(" = ");
                } else {
                    result.append(numA);
                    result.append(" x ");
                    result.append(numB);
                    result.append(" + ");
                    result.append(numAdd);
                    result.append(" = ");
                }
                break;
            }
        }
        return result.toString();
    }

    /**
     * 获取除法减法算式
     */
    public static String getDivisionRed(String type) {
        StringBuffer result = new StringBuffer();
        Random random = new Random();
        int numRed = random.nextInt(50)+50;
        int numRed2 = random.nextInt(10);
        for (int j = 0; j < 30000; j++) {
            int numA = random.nextInt(32)+60;
            int numB = random.nextInt(10);
            if (numA==0||numB == 0){
                continue;
            }
            if (numA / numB > 0 && numA / numB < 10 && numA % numB == 0) {
                if ("6".equals(type) && numRed - numA / numB >= 0) {
                    result.append(" ");
                    result.append(numRed);
                    result.append(" - ");
                    result.append(numA);
                    result.append(" ÷ ");
                    result.append(numB);
                    result.append(" = ");
                } else if ("7".equals(type) && numA / numB - numRed2 >= 0) {
                    result.append(numA);
                    result.append(" ÷ ");
                    result.append(numB);
                    result.append(" - ");
                    result.append(numRed2);
                    result.append(" = ");
                }
                break;
            }
        }
        return result.toString();
    }

    /**
     * 获取除法加法算式
     */
    public static String getDivisionAdd(String type) {
        StringBuffer result = new StringBuffer();
        Random random = new Random();
        for (int j = 0; j < 500; j++) {
            int numAdd = random.nextInt(100);
            int numA = random.nextInt(82);
            int numB = random.nextInt(10);
            if (numA  == 0|| numB == 0){
                continue;
            }
            if (numA / numB > 0 && numA / numB < 10 && numA % numB == 0 && numA / numB + numAdd < 101) {
                result.append(" ");
                if ("4".equals(type)) {
                    result.append(numAdd);
                    result.append(" + ");
                    result.append(numA);
                    result.append(" ÷ ");
                    result.append(numB);
                    result.append(" = ");
                } else if ("5".equals(type)) {
                    result.append(numA);
                    result.append(" ÷ ");
                    result.append(numB);
                    result.append(" + ");
                    result.append(numAdd);
                    result.append(" = ");
                }
                break;
            }
        }
        return result.toString();
    }

    public static void textToFile(String path, final String strBuffer) {
        try {
            // 创建文件对象
            File fileText = new File(path);
            // 向文件写入对象写入信息
            FileWriter fileWriter = new FileWriter(fileText);

            // 写文件
            fileWriter.write(strBuffer);
            // 关闭
            fileWriter.close();
        } catch (IOException e) {
            //
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值