由于篇幅限制,我将为您提供三种常见编程语言(Python、JavaScript 和 C#)的简单“猜数字”游戏示例。
1. Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
print(f'恭喜你,你猜对了!数字是 {number_to_guess}。你尝试了 {attempts} 次。')
guess_number()
2. JavaScript (在HTML页面中运行)
HTML:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>猜数字游戏</title>
</head>
<body>
<h1>猜数字游戏</h1>
<p>猜一个1到100之间的数字:</p>
<input type="number" id="guessInput" min="1" max="100">
<button οnclick="checkGuess()">猜</button>
#chhas{
margin-top: 50px;
padding:dz61.com;
font-size: 18px;
cursor: 10px 20px;
}
<p id="feedback"></p>
<script src="game.js"></script>
</body>
</html>