10以内加减法算式生成(3个运算数)

273 阅读1分钟
输出样例
  • ( ) + 3 - 3 = 6 | ( ) + 7 - 4 = 7
  • ( ) + 5 - 3 = 5 | ( ) - 8 + 8 = 4
  • ( ) - 2 + 3 = 4 | 2 + ( ) - 7 = 1
  • 3 + ( ) - 6 = 2 | 5 - ( ) + 5 = 4
  • 6 + ( ) - 7 = 3 | 8 + ( ) - 9 = 2
  • ( ) + 4 - 3 = 7 | 2 - 3 + ( ) = 1
  • 8 + 2 - ( ) = 1 | 8 - ( ) + 1 = 1
代码
package com.css.bdpfnew;


import java.util.*;

/**
 * 10以内加减法算式生成(3个运算数)
 * @author erDuo
 * @date 2022/11/10 9:16
 */
public class MathAddSubPractice {

    // 运算数 统计
    public static HashMap<Object, Integer> OPERATOR_COUNT = new HashMap<Object, Integer>() {
        {
            put(1, 0);
            put(2, 0);
            put(3, 0);
            put(4, 0);
            put(5, 0);
            put(6, 0);
            put(7, 0);
            put(8, 0);
            put(9, 0);
            put("+", 0);
            put("-", 0);
        }
    };

    // 算式输出
    public static ArrayList<String> formulaList = new ArrayList<>();

    public static void main(String[] args) {

        int i = 0;
        while(i < 50000) {
            // 算式生成 三个运算数 确认 其中一个运算数
            // new MathAddSubPractice().questionGenToConfirmOperand();

            // 算式生成 三个运算数 确认 结果
            new MathAddSubPractice().questionGenToConfirmResult();
            i++;
        }

        // 保证一行两个
        if (formulaList.size() % 2 !=0) {
            formulaList.remove(formulaList.size()-1);
        }

        // 算式输出
        for (int j = 0; j < formulaList.size(); j = j + 2 ) {
            String formula1 = formulaList.get(j);
            String formula2 = formulaList.get(j + 1);
            System.out.println(String.format("%s    |    %s", formula1, formula2));
        }

        // 运算数 算子 统计
        for (Map.Entry<Object, Integer> entry : OPERATOR_COUNT.entrySet()) {
            String count = String.format("%s : %d", entry.getKey(), entry.getValue());
            System.out.println(count);
        }
    }


    /**
     * 算式生成 三个运算数 确认 结果
     */
    public void questionGenToConfirmResult() {
        // 随机 加减 操作符
        int ran1 = new Random().nextInt(2);
        int ran2 = new Random().nextInt(2);

        // 加减操作符 三个 操作数
        String operator1 = "*";
        int operand1 = new Random().nextInt(9) + 1;
        String[] operator2 = new String[]{"+", "-"};
        int operand2 = new Random().nextInt(9) + 1;
        String[] operator3 = new String[]{"+", "-"};
        int operand3 = new Random().nextInt(9) + 1;
        int result = new Random().nextInt(9) + 1;

        // 操作符 操作数 列表
        List<Object> mathList = new ArrayList<>();

        mathList.add(operand1);
        mathList.add(operand2);
        mathList.add(operand3);
        mathList.add(operator1);
        mathList.add(operator2[ran1]);
        mathList.add(operator3[ran2]);
        mathList.add(result);

        int operand12 = compute((int)mathList.get(0), (int)mathList.get(1), (String)mathList.get(4));
        int operand123 = compute(operand12, (int)mathList.get(2), (String)mathList.get(5));

        if (operand123 == result && operand12 > 0) {
            // 统计 运算数 算子
            for (Object o : mathList) {
                if (OPERATOR_COUNT.containsKey(o)) {
                    OPERATOR_COUNT.put(o, OPERATOR_COUNT.get(o) + 1);
                }
            }
            // 随机 求值 的位置
            mathList.set(6, "(   )");
            String formula = String.format("%s %s %s %s %s = %s", mathList.get(0), mathList.get(4), mathList.get(1), mathList.get(5), mathList.get(2), mathList.get(6));
            formulaList.add(formula);

        }
    }


    /**
     * 算式生成 三个运算数 确认 其中一个运算数
     */
    public void questionGenToConfirmOperand() {
        // 随机 加减 操作符
        int ran1 = new Random().nextInt(2);
        int ran2 = new Random().nextInt(2);

        // 加减操作符 三个 操作数
        String operator1 = "*";
        int operand1 = new Random().nextInt(9) + 1;
        String[] operator2 = new String[]{"+", "-"};
        int operand2 = new Random().nextInt(9) + 1;
        String[] operator3 = new String[]{"+", "-"};
        int operand3 = new Random().nextInt(9) + 1;
        int result = new Random().nextInt(9) + 1;

        // 操作符 操作数 列表
        List<Object> mathList = new ArrayList<>();

        mathList.add(operand1);
        mathList.add(operand2);
        mathList.add(operand3);
        mathList.add(operator1);
        mathList.add(operator2[ran1]);
        mathList.add(operator3[ran2]);
        mathList.add(result);

        int operand12 = compute((int)mathList.get(0), (int)mathList.get(1), (String)mathList.get(4));
        int operand123 = compute(operand12, (int)mathList.get(2), (String)mathList.get(5));

        if (operand123 == result) {
            // 统计 运算数 算子
            for (Object o : mathList) {
                if (OPERATOR_COUNT.containsKey(o)) {
                    OPERATOR_COUNT.put(o, OPERATOR_COUNT.get(o) + 1);
                }
            }

            mathList.set(3, "+");

            // 随机 求值 的位置
            int ran3 = new Random().nextInt(3);
            mathList.set(ran3, "(   )");


            List<Integer> leftOpList = new ArrayList<>();

            for (int i = 0; i < 3; i++) {
                if (i != ran3) {
                    leftOpList.add(i);
                }
            }

            Integer resultX = 0;

            for (Integer op: leftOpList) {
                // 操作符
                String operator = (String)mathList.get(op + 3);
                Integer plusOrMinus = 0;

                if (operator.equals("+")) {
                    plusOrMinus = 1;
                } else {
                    plusOrMinus = -1;
                }

                Integer operand = (Integer)mathList.get(op);

                resultX += operand * plusOrMinus;
            }

            if (resultX > 0) {
                String formula = String.format("%s %s %s %s %s = %s", mathList.get(0), mathList.get(4), mathList.get(1), mathList.get(5), mathList.get(2), mathList.get(6));
                formulaList.add(formula);
            }
        }
    }

    /**
     * 加减法计算
     * @param a 运算数1
     * @param b 运算数2
     * @param x 操作符
     * @return 结果
     */
    public int compute(int a, int b, String x) {
        if (x == "+") {
            return Math.addExact(a, b);
        }
        return Math.subtractExact(a, b);
    }
}