用html 写一个猜数字游戏
时间: 2023-10-04 14:06:30 浏览: 51
<!DOCTYPE html>
<html>
<head>
<title>猜数字游戏</title>
<style>
h1 {
text-align: center;
}
</style>
</head>
<body>
<h1>猜数字游戏</h1>
<p>请猜一个1到100的数字:</p>
<input type="text" id="guess" name="guess">
<button onclick="checkGuess()">提交</button>
<p id="result"></p>
<script>
var answer = Math.floor(Math.random()*100)+1;
var count = 0;
function checkGuess() {
var guess = document.getElementById("guess").value;
if (guess == answer) {
document.getElementById("result").innerHTML = "恭喜你猜对了!共猜了" + count + "次。";
} else if (guess > answer) {
document.getElementById("result").innerHTML = "你猜的数字太大了,请再猜一次。";
count++;
} else {
document.getElementById("result").innerHTML = "你猜的数字太小了,请再猜一次。";
count++;
}
}
</script>
</body>
</html>
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)