设计一个猜数字游戏如下:
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<a>猜数字游戏:</a><br/>
<input type="test" id="inputId" value="" />
<button onclick="guess()">猜</button>
<input type="test" id="resultId" value="" />
<script type="text/javascript">
var random = parseInt(Math.random() * 100 + 1); // 随机生成一个1~100的整数
function guess() {
var inputObj = document.getElementById('inputId');
var result = parseInt(inputObj.value);
if (result > random) {
var resultObj = document.getElementById('resultId');
resultObj.value = "大了";
} else if (result < random) {
var resultObj = document.getElementById('resultId');
resultObj.value = "小了";
} else if (result === random) {
var resultObj = document.getElementById('resultId');
resultObj.value = "猜对了";
}
}
</script>
</body>
</html>