成品是以htm为后缀的编程。设计一个简单的算术考试程序,首先用随机函数产生一个简单的算术式显示在文本框中,等待用户在另一文本框中输入答案,同时打开定时器,控制每题的答题时间,当用户输入答案后单击完成,程序自动判断正确性,并显示得分。
编程问题,不会做。
答案:1 悬赏:0 手机版
解决时间 2021-04-22 02:56
- 提问者网友:温旧梦泪无声
- 2021-04-21 22:00
最佳答案
- 五星知识达人网友:妄饮晩冬酒
- 2021-04-21 23:15
把下面的代码另存为xxx.htm再双击执行就可以了。
<html>
<head>
<title>整数四则运算测试</title>
<script>
var testSecond = 15;
var remainSecond = testSecond;
var testMaxNumber = 10;
var patterns = ["+","-", "*", "/"];
var timer = 0;
function startTest(){
document.getElementById("start").disabled = true;
document.getElementById("answer").disabled = false;
document.getElementById("judge").disabled = false;
document.getElementById("question").innerText = generateQuestion();
remainSecond = testSecond;
document.getElementById("timer").innerText = "输入答案(剩余时间" + remainSecond + "秒):";
document.getElementById("answer").value = "";
timer = setTimeout(timeout, 1000);
}
function getRandom(max){
return Math.floor(Math.random()*max);
}
function generateQuestion(){
var firstNum = getRandom(testMaxNumber);
var pattern = patterns[getRandom(patterns.length)];
var secondNum = getRandom(testMaxNumber);
if("/" == pattern){
if(0 == secondNum){
//0不能作为被除数
secondNum ++;
}
//使得结果为整数
firstNum = getRandom(Math.ceil(testMaxNumber/secondNum)) * secondNum;
}
return firstNum +" " + pattern + " " + secondNum;
}
function judge(){
if(0!=timer){clearTimeout(timer);}
document.getElementById("start").disabled = false;
document.getElementById("answer").disabled = true;
document.getElementById("judge").disabled = true;
var answer = document.getElementById("answer").value;
var realAnswer = eval(document.getElementById("question").innerText);
var result = document.getElementById("result");
if ("" == answer){
result.innserText = "未答题。";
}else if(realAnswer == (answer - 0)){
result.innerText = "恭喜你,答对了!";
}else{
result.innerText = "很遗憾,回答错误。正确答案是:" + realAnswer;
}
}
function timeout(){
remainSecond -- ;
if(remainSecond == -1){
document.getElementById("timer").innerText = "时间到。";
judge();
}else{
document.getElementById("timer").innerText = "输入答案(剩余时间" + remainSecond + "秒):";
timer = setTimeout(timeout, 1000);
}
}
</script>
</head>
<body>
<input id="start" type="button" value="开始测试" onclick="startTest()"/>
<br>
算式:<font id="question"></font>
<br>
<font id="timer">输入答案(剩余时间15秒):</font>
<input id="answer" type="text" disabled="true"/>
<br>
<input id="judge" type="button" value="查看结果" onclick="judge();" disabled="true"/>
<font id="result"></font>
</body>
</html>
<html>
<head>
<title>整数四则运算测试</title>
<script>
var testSecond = 15;
var remainSecond = testSecond;
var testMaxNumber = 10;
var patterns = ["+","-", "*", "/"];
var timer = 0;
function startTest(){
document.getElementById("start").disabled = true;
document.getElementById("answer").disabled = false;
document.getElementById("judge").disabled = false;
document.getElementById("question").innerText = generateQuestion();
remainSecond = testSecond;
document.getElementById("timer").innerText = "输入答案(剩余时间" + remainSecond + "秒):";
document.getElementById("answer").value = "";
timer = setTimeout(timeout, 1000);
}
function getRandom(max){
return Math.floor(Math.random()*max);
}
function generateQuestion(){
var firstNum = getRandom(testMaxNumber);
var pattern = patterns[getRandom(patterns.length)];
var secondNum = getRandom(testMaxNumber);
if("/" == pattern){
if(0 == secondNum){
//0不能作为被除数
secondNum ++;
}
//使得结果为整数
firstNum = getRandom(Math.ceil(testMaxNumber/secondNum)) * secondNum;
}
return firstNum +" " + pattern + " " + secondNum;
}
function judge(){
if(0!=timer){clearTimeout(timer);}
document.getElementById("start").disabled = false;
document.getElementById("answer").disabled = true;
document.getElementById("judge").disabled = true;
var answer = document.getElementById("answer").value;
var realAnswer = eval(document.getElementById("question").innerText);
var result = document.getElementById("result");
if ("" == answer){
result.innserText = "未答题。";
}else if(realAnswer == (answer - 0)){
result.innerText = "恭喜你,答对了!";
}else{
result.innerText = "很遗憾,回答错误。正确答案是:" + realAnswer;
}
}
function timeout(){
remainSecond -- ;
if(remainSecond == -1){
document.getElementById("timer").innerText = "时间到。";
judge();
}else{
document.getElementById("timer").innerText = "输入答案(剩余时间" + remainSecond + "秒):";
timer = setTimeout(timeout, 1000);
}
}
</script>
</head>
<body>
<input id="start" type="button" value="开始测试" onclick="startTest()"/>
<br>
算式:<font id="question"></font>
<br>
<font id="timer">输入答案(剩余时间15秒):</font>
<input id="answer" type="text" disabled="true"/>
<br>
<input id="judge" type="button" value="查看结果" onclick="judge();" disabled="true"/>
<font id="result"></font>
</body>
</html>
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯