JS综合案例:随机答题的小游戏

来自CloudWiki
跳转至: 导航搜索
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>答题小游戏案例示例</title>
</head>
<body>
<pre>
<script type="text/javascript">
 var x,y,result,answer,go_on;
	while(true)
	{//先出题,再答题
		x = Math.floor(Math.random() *90)+10; //随机生成两个二位整数
		y = Math.floor(Math.random() *90)+10;
		result = x+y; //计算标准答案
		answer = parseFloat(prompt(x + "+" + y + "=","0")); //接收用户答案
		go_on = confirm((answer==result?"答对":"答错")+"!\t继续测试吗?");
		if(!go_on) break; //若单击“取消”按钮则返回 false,即结束提问
	}
</script>
</pre>
</body>
</html>