function generateQuestion(numbers) {
let result = 0;
const operators = ['+', '-'];
let question = '';
let lastOperator = '+'; // 初始操作符为加号
let lastNumber = null; // 初始化上一个数字为null
let buttonsDisabled = true; // 按钮初始状态为不可点击
for (let i = 0; i < 2; i++)
{
let number, operator;
let validNumber = false;
let result_onesDigit=onesDigit(result);
let result_tensDigit = tensDigit(result);
let result_hundredDigit = hundredDigit(result);
// 循环直到找到一个合适的数字
do {
if(type==1)//1-4直加直减
{
number = Math.floor(Math.random() * 4) + 1; // 1-4的随机数
operator = operators[Math.floor(Math.random() * 2)];
// 检查当前操作符和数字是否会导致结果超出范围
if (operator === '+') {
validNumber = result + number <= 4;
}
else
{
validNumber = result - number >= 0;
}
}
} while (!validNumber);
lastOperator = operator; // 更新上一个操作符
lastNumber = number; // 更新上一个数字
setTimeout(() =>
{
displayQuestion(number,'','');
}, (i + 1) * shijian*1000);
result += operator === '+' ? number : -number;
question += (i > 0 ? ' ' : '') + operator + number;
}
questions.push({ question: question, answer: result });
}
上面是随机两个数字做加减法,并且数字是一个一个的显示。
现在想要做乘除法,数字和符号一起显示,并且第二个数字固定是2