安洁我废话不多说了喵,上源码
<html>
<head>
<title>AnJProject-猜数字游戏</title>
<meta charset="UTF-8">
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
margin-top: 50px;
}
input {
padding: 10px;
font-size: 20px;
margin-top: 20px;
}
button {
padding: 10px 20px;
font-size: 20px;
margin-top: 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>猜数字游戏</h1>
<p>在1到100之间猜一个数字</p>
<input type="text" id="guess" pattern="[0-9]+" required onkeydown="if(event.keyCode == 13) checkGuess()">
<button onclick="checkGuess()">猜!</button>
<p id="result"></p>
<script>
var answer = Math.floor(Math.random() * 100) + 1; // 生成1到100之间的随机数
var attempts = 0;
function checkGuess() {
var guess = parseInt(document.getElementById("guess").value);
if (guess == answer) {
document.getElementById("result").innerHTML = "恭喜你,猜对了!你用了" + attempts + "次猜中了答案!";
} else if (guess < answer) {
document.getElementById("result").innerHTML = "你猜的数字太小了,请再试一次。";
attempts++;
} else if (guess > answer) {
document.getElementById("result").innerHTML = "你猜的数字太大了,请再试一次。";
attempts++;
}
}
</script>
<footer>
<p>© 2023 AnJProject. All rights reserved.</p>
</footer>
</body>
</html>
文章有(0)条网友点评
关闭网页右键功能,添加复制版权提醒代码
阅读更多用JS方式给网站添加已运行多少天的代码
阅读更多给博客网站添加鼠标点击文字特效
阅读更多