加减乘除的运算测试
要完成此效果需要两个步骤
第一步:把如下代码加入到<head>区域中
<SCRIPT LANGUAGE="JavaScript"> <!-- Begin correct=0; wrong=0;
function random(maxValue) { day= new Date(); hour= day.getHours(); min=day.getMinutes(); sec=day.getSeconds(); mili=day.getTime() return(((hour*3600)+(min*60)+(sec)+mili) % maxValue); } function ranom(maxValue) { day= new Date(); mil=day.getTime(); return((mil) % maxValue); }
function add() { if(document.quizform.arithmetic[0].checked) maxValue=10; else { if(document.quizform.arithmetic[1].checked) maxValue=30; else { maxValue=60; } } numA=random(maxValue); numB=ranom(maxValue); numC=numA + numB; Answer=window.prompt( numA + "+" + numB + " = ", ""); ans(); } function subtract() { if(document.quizform.arithmetic[0].checked) maxValue=10; else {if(document.quizform.arithmetic[1].checked) maxValue=30; else { maxValue=60 } } numA=random(maxValue); numB=ranom(maxValue); numC=numA - numB; Answer=window.prompt( numA + "-" + numB+ " = ", 0); ans() } function divide() { if(document.quizform.arithmetic[0].checked) maxValue=10; else { if(document.quizform.arithmetic[1].checked) maxValue=30; else { maxValue=60 } } numA=random(maxValue)+1; numB=ranom(maxValue)+1; numC=numA / numB; numC=Math.round(numC) window.alert("Please round your answer off:\n" +".5 or higher rounds one number up\n" +".4 or lower rounds one number down"); Answer=window.prompt( numA + "/" + numB + " = ", 0); ans() } function multiply() { if(document.quizform.arithmetic[0].checked) maxValue=10; else { if(document.quizform.arithmetic[1].checked) maxValue=30; else { maxValue=60 } } numA=random(maxValue); numB=ranom(maxValue); numC=numA * numB; Answer=window.prompt( numA + "*" + numB + " = ", 0); ans(); } function check() { if ((correct+wrong) != 0) { score = "" + ((correct / (correct + wrong)) * 100); score = score.substring(0,4) + "%"; alert("你的答题正确率是: " + score + "\n" + correct + " 次答对\n" + wrong + " 次答错") } else alert("你还未完成任一习题."); } function ans() { if (Answer == numC) { correct++; msg = "恭喜, 你回答正确."; } else { wrong++; msg = "回答错误! " + Answer + " 这个答案不对.\n\n" + "正确答案应该是 " +numC + "."; } score = "" + ((correct / (correct + wrong)) * 100); score = score.substring(0,4) + "%"; alert(msg + "\n\n你的正确率: " + score + "\n" + correct + " 次答对\n" + wrong + " 次答错") } // End --> </script>
第二步:把如下代码加入到<body>区域中
<center> <form name=quizform> <input type=button value="加法运算" onClick="add()"> <input type=button value="减法运算" onClick="subtract()"> <input type=button value="乘法运算" onClick="multiply()"> <input type=button value="除法运算" onClick="divide()"> <br> <br> <input type="radio" name="arithmetic"> 低级难度 <input type="radio" name="arithmetic" checked>
|