随机生成四则运算

<!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>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值