某学校需要一个能给学领前儿童自动出三个数加减法的算术题的程序,要求在运算过程中只能使用20以内的加减法且不能出现负数,并且未知数可以在等式的任何一位, 如 3 + 4 – 2 = ( ), 8 – ( ) + 6 = 12等。请你用任意一种编程语言实现该出题程序并输出到屏幕。
var goflag = false;
var mygo = function () {
sign_list = [-1, 1]
sign_2 = sign_list[Math.round(Math.random())];
sign_3 = sign_list[Math.round(Math.random())];
number_list = [];
for (i = 0; i < 3; i++) {
number_list[i] = Math.round(Math.random() * 19) + 1
}
ans = number_list[0] + sign_2 * number_list[1] + sign_3 * number_list[2]
go_list = [number_list[0], sign_2 * number_list[1], sign_3 * number_list[2]]
boolflag = Math.round(Math.random() * 2) + 1
// console.log(boolflag)
teststr = number_list[0] + ""
for (i = 1; i < 3; i++) {
if (i == boolflag) {
teststr = go_list[i] > 0 ? teststr + '+()' : teststr + '-()'
} else {
teststr = go_list[i] > 0 ? teststr + `+${go_list[i]}` : teststr + `${go_list[i]}`;
}
}
teststr = boolflag == 3 ? teststr + `=()` : teststr + `=${ans}`
if (ans >= 0 && ans <= 20) {
goflag = true
console.log(teststr)
}
}
while (!goflag) {
mygo()
}