- Python 版本
python
import random
def guess_the_number(): e-energy.cn
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("猜数字游戏!我已经想好了一个1到100之间的数字。")
while guess != number_to_guess:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜!你猜对了,数字就是{number_to_guess}。")
print(f"你总共尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_the_number()
2. JavaScript (HTML + JavaScript) 版本
html
猜数字游戏
我已经想好了一个1到100之间的数字。你能猜到是哪个吗?
猜一猜<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById('guess').value);
attempts++;
if (isNaN(guess)) {
document.getElementById('result').innerText = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
document.getElementById('result').innerText = "太小了!";
} else if (guess > numberToGuess) {
document.getElementById('result').innerText = "太大了!";
} else {
document.getElementById('result').innerText = `恭喜!你猜对了,数字就是${numberToGuess}。你总共尝试了${attempts}次。`;
}
}
</script>
3. C# 版本 (控制台应用程序) csharp using System;
class Program
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess;
int attempts = 0;
Console.WriteLine("猜数字游戏!我已经想好了一个1到100之间的数字。");
do
{
Console.Write("请输入你的猜测:");
bool isValid = int.TryParse(Console.ReadLine(), out guess);
if (!isValid)
{
Console.WriteLine("请输入一个有效的数字。");
continue;
}
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
else
{
Console.WriteLine($"恭喜!你猜对了,数字就是{numberToGuess}。你总共尝试了{attempts}次。");
}
} while (guess != numberToGuess);
}
}
每个示例都展示了如何使用不同的编程语言来编写一个简单的猜数字游戏。希望这些示例对你有帮助!由于篇幅限制,我将分别为你提供三种流行编程语言(Python、JavaScript(用于网页)、C#)的示例,用于创建一个简单的小游戏:猜数字游戏。这个游戏会随机生成一个1到100之间的数字,玩家需要猜测这个数字,直到猜中为止。
- Python 版本
python
import random
def guess_the_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("猜数字游戏!我已经想好了一个1到100之间的数字。")
while guess != number_to_guess:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜!你猜对了,数字就是{number_to_guess}。")
print(f"你总共尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_the_number()
2. JavaScript (HTML + JavaScript) 版本
html
猜数字游戏
我已经想好了一个1到100之间的数字。你能猜到是哪个吗?
猜一猜<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById('guess').value);
attempts++;
if (isNaN(guess)) {
document.getElementById('result').innerText = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
document.getElementById('result').innerText = "太小了!";
} else if (guess > numberToGuess) {
document.getElementById('result').innerText = "太大了!";
} else {
document.getElementById('result').innerText = `恭喜!你猜对了,数字就是${numberToGuess}。你总共尝试了${attempts}次。`;
}
}
</script>
3. C# 版本 (控制台应用程序) csharp using System;
class Program
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess;
int attempts = 0;
Console.WriteLine("猜数字游戏!我已经想好了一个1到100之间的数字。");
do
{
Console.Write("请输入你的猜测:");
bool isValid = int.TryParse(Console.ReadLine(), out guess);
if (!isValid)
{
Console.WriteLine("请输入一个有效的数字。");
continue;
}
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
else
{
Console.WriteLine($"恭喜!你猜对了,数字就是{numberToGuess}。你总共尝试了{attempts}次。");
}
} while (guess != numberToGuess);
}
}
每个示例都展示了如何使用不同的编程语言来编写一个简单的猜数字游戏。希望这些示例对你有帮助!