<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>100以内四则元算</title>
<style>
#wrap{
width:250px;
margin:0 auto;
padding: 0;
position: relative;
top:100px;
}
</style>
</head>
<body>
<div id="wrap">
<div id="Div1">
<label for="" id="timu"></label><input type="button" id="chuti" value="出题" οnclick="Chuti()" />
</div>
<div id="Div2">
<input type="text" id="daan" placeholder="请输入答案..." /><input type="button" id="queding" value="确定" οnclick="duidaan()" />
</div>
</div>
<script type="text/javascript">
var timu = document.getElementById("timu");
var daan = document.getElementById("daan");
var fuhao = ["+","-","*","÷"];
var zhengquedaan = -10000;
function Chuti(){
//两个操作数
var d1,d2;
//运算符
var f;
while(true){
d1 = Math.floor(Math.random()*101); //random生成 [0,1) 的随机数 floor(x) : 取小于等于x的最大整数
d2 = Math.floor(Math.random()*101);
f = fuhao[Math.floor(Math.random()*4)];
switch(f){
case "+":
zhengquedaan = d1 + d2;
break;
case "-":
zhengquedaan = d1 - d2;
break;
case "*":
zhengquedaan = d1 * d2;
break;
case "÷":
if(d2 === 0){
zhengquedaan = -1;
}else{
zhengquedaan = d1 / d2;
}
break;
}
//判断结果是否在0 ~ 100以内
if(zhengquedaan > 100 || zhengquedaan < 0){
continue;
}else if(zhengquedaan%1 !== 0){//结果是否为整数
continue;
}
else{
break;
}
}
timu.innerText = d1 + " " + f + " " + d2 + " = ? ";
}
function duidaan(){
if(zhengquedaan === -10000){
alert("亲,先点击出题按钮!");
}else if(daan.value === (zhengquedaan + "")){
alert("恭喜!运算正确!");
}else{
alert("运算错误!正确答案为: " + zhengquedaan);
}
}
</script>
</body>
</html>