function generateQuestion(numbers) {
let result = 0;
let question = '';
let buttonsDisabled = true; // 按钮初始状态为不可点击
if(numbers!='')//假如numbers='+8 -4 +2',运算结果是6,但是提示答案是2
{
// 将字符串按空格分割成数组
let parts = numbers.split(' ');
// 遍历数组,处理每个数字和运算符
for (let i = 0; i < parts.length; i++) {
let part = parts[i];
// 确定当前部分是数字还是运算符
let isNumber = !isNaN(part);
let number, operator;
if (isNumber) {
number = parseInt(part, 10);
// 如果不是第一个数字,则获取前一个元素作为运算符
if (i > 0) {
operator = parts[i - 1];
}
} else {
operator = part;
}
// 每隔一定时间显示数字和运算符
setTimeout(() => {
displayQuestion(number,'','');
}, (i + 1) * shijian * 1000);
// 更新结果和问题字符串
if (i > 0 && isNumber) {
result += operator === '+' ? number : -number;
question += ' ' + operator + number;
}
}
// 添加第一个数字到问题字符串
question = parts[0] + question;
}
questions.push({ question: question, answer: result });
}
function checkAnswer() {
if($.trim($("input[name=answerInput]").val()) == "")
{
$("input[name=answerInput]").focus();
return false;
}
document.getElementById('question1').textContent='';
isQuestionDisplayed = false;//每一题重新禁用按纽
const userAnswer = parseInt(document.getElementById('answerInput').value);
if (userAnswer === questions[currentQuestionIndex].answer) {
alert('正确!');
} else {
alert('错误,正确答案是: ' + questions[currentQuestionIndex].answer);
}
document.getElementById('answerInput').value = '';
$("input[name=answerInput]").focus();
if (userAnswer !== undefined) { // 确保 userAnswer 已被定义
questions[currentQuestionIndex].userAnswer = userAnswer; // 添加这一行
}
if (userAnswer === questions[currentQuestionIndex].answer) {
questions[currentQuestionIndex].isCorrect = true;
} else {
questions[currentQuestionIndex].isCorrect = false;
}
if(tiliang==1)
{
if (currentQuestionIndex < zushu-1) {//几道题
currentQuestionIndex++;
document.getElementById('question').textContent = '';
generateQuestion(numbers);
} else {
let endTime = new Date().getTime(); // 记录结束时间
let totalTime = (endTime - startTime) / 1000; // 转换为秒
localStorage.setItem('questionsData', JSON.stringify(questions));
window.location.href = "/oa/oa_zxsxl.php?act=jieguo&totalTime="+totalTime+"&type={$type}&type_text={$type_text}&xingshi={$xingshi}&xingshi_text={$xingshi_text}&tiliang={$tiliang}&zushu={$zushu}&bishu={$bishu}&shijian={$shijian}&yusu={$yusu}";
}
}
else
{
currentQuestionIndex++;
document.getElementById('question').textContent = '';
generateQuestion(numbers);
}
if(xingshi==1 || xingshi==2)
{
document.getElementById('question').textContent = '请看题';
}
else
{
document.getElementById('question').textContent = '请听题';
speak1('请听题');
}
}
1、假如numbers='+8 -4 +2',运算结果是6,但是提示答案是2
2、符号+没有显示,第一个数字可以不显示+号,但后面的+号要显示