输出样例
-
7 + 8 - 4 = ( ) | 5 - 3 + 7 = ( )
-
18 - 5 - 11 = ( ) | 16 - 12 + 2 = ( )
-
12 - 10 + ( ) = 13 | 4 + ( ) + 11 = 17
-
3 + 11 - ( ) = 10 | 10 - 8 + ( ) = 18
-
2 + 16 - ( ) = 13 | 9 + 3 - ( ) = 6
-
( ) - 14 + 2 = 3 | 15 + ( ) + 1 = 19
-
5 + 6 - ( ) = 4 | ( ) - 4 - 2 = 7
代码
package com.erduo.duolaiapp.data.util;
import java.util.*;
/**
* 20以内加减法算式生成(3个运算数)
* @author erDuo
* @date 2024/01/25 15:05
*/
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 ArrayList<String> formulaListOther = new ArrayList<>();
public static void main(String[] args) {
int i = 0;
while(i < 100000) {
// 算式生成 三个运算数 确认 结果
new MathAddSubPractice().questionGenToConfirmResult(6);
new MathAddSubPractice().questionGenToConfirmResult(new Random().nextInt(3));
i++;
}
// 保证一行两个
if (formulaList.size() % 2 !=0) {
formulaList.remove(formulaList.size()-1);
}
// 保证一行两个
if (formulaListOther.size() % 2 !=0) {
formulaListOther.remove(formulaListOther.size()-1);
}
int count1 = formulaList.size() / 44;
int count2 = formulaListOther.size() / 44;
int countMin = Math.min(count1, count2);
for (int k = 0; k < countMin; k++) {
// 算式输出
for (int j = 0; j < 44; j = j + 2 ) {
String formula1 = formulaList.get(j + k * 44);
String formula2 = formulaList.get(j + 1 + k * 44);
System.out.printf("%s | %s%n", formula1, formula2);
}
for (int j = 0; j < 44; j = j + 2 ) {
String formula1 = formulaListOther.get(j + k * 44);
String formula2 = formulaListOther.get(j + 1 + k * 44);
System.out.printf("%s | %s%n", 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);
}
}
/**
* 算式生成 三个运算数 确认 结果
* xIndex 求值索引 6: 最后结果; 1, 2, 3 操作数
*/
public void questionGenToConfirmResult(int xIndex) {
// 随机 加减 操作符
int ran1 = new Random().nextInt(2);
int ran2 = new Random().nextInt(2);
// 加减操作符 三个 操作数
String operator1 = "*";
int operand1 = new Random().nextInt(19) + 1;
String[] operator2 = new String[]{"+", "-"};
int operand2 = new Random().nextInt(19) + 1;
String[] operator3 = new String[]{"+", "-"};
int operand3 = new Random().nextInt(19) + 1;
int result = new Random().nextInt(19) + 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 && operand12 < 20) {
// 统计 运算数 算子
for (Object o : mathList) {
if (OPERATOR_COUNT.containsKey(o)) {
OPERATOR_COUNT.put(o, OPERATOR_COUNT.get(o) + 1);
}
}
// 随机 求值 的位置
mathList.set(xIndex, "( )");
String formula = String.format("%2s %2s %2s %2s %2s = %2s", mathList.get(0), mathList.get(4), mathList.get(1), mathList.get(5), mathList.get(2), mathList.get(6));
if (xIndex == 6) {
formulaList.add(formula);
} else {
formulaListOther.add(formula);
}
}
}
/**
* 加减法计算
* @param a 运算数1
* @param b 运算数2
* @param x 操作符
* @return 结果
*/
public int compute(int a, int b, String x) {
if (Objects.equals(x, "+")) {
return Math.addExact(a, b);
}
return Math.subtractExact(a, b);
}
}