- Python: 猜数字游戏
python
import random
def guess_number_game(): jzsafe.com
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
except ValueError:
print('请输入一个整数!')
print(f'恭喜你,猜对了!数字是 {number_to_guess}。你用了 {attempts} 次尝试。')
if name == “main”:
guess_number_game()
2. JavaScript (在Node.js或浏览器控制台中运行): 猜数字游戏
javascript
function guessNumberGame() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
do {
guess = prompt('猜一个1到100之间的数字:');
attempts++;
guess = parseInt(guess);
if (isNaN(guess)) {
alert('请输入一个整数!');
continue;
}
if (guess < numberToGuess) {
alert('太小了!');
} else if (guess > numberToGuess) {
alert('太大了!');
}
} while (guess !== numberToGuess);
alert(`恭喜你,猜对了!数字是 ${numberToGuess}。你用了 ${attempts} 次尝试。`);
}
guessNumberGame();
3. C# (在控制台应用程序中运行): 猜数字游戏
csharp
using System;
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess)
{
Console.Write("猜一个1到100之间的数字: ");
if (!int.TryParse(Console.ReadLine(), out guess))
{
Console.WriteLine("请输入一个整数!");
continue;
}
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
}
Console.WriteLine($"恭喜你,猜对了!数字是 {numberToGuess}。你用了 {attempts} 次尝试。");
}
}
这些示例都是基于控制台的简单猜数字游戏。你可以根据自己的需要修改和扩展它们。由于篇幅限制,我将分别为你提供三种流行编程语言(Python、JavaScript、C#)的简单小游戏代码示例。这些示例都是基于控制台的简单游戏。
- Python: 猜数字游戏
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
except ValueError:
print('请输入一个整数!')
print(f'恭喜你,猜对了!数字是 {number_to_guess}。你用了 {attempts} 次尝试。')
if name == “main”:
guess_number_game()
2. JavaScript (在Node.js或浏览器控制台中运行): 猜数字游戏
javascript
function guessNumberGame() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
do {
guess = prompt('猜一个1到100之间的数字:');
attempts++;
guess = parseInt(guess);
if (isNaN(guess)) {
alert('请输入一个整数!');
continue;
}
if (guess < numberToGuess) {
alert('太小了!');
} else if (guess > numberToGuess) {
alert('太大了!');
}
} while (guess !== numberToGuess);
alert(`恭喜你,猜对了!数字是 ${numberToGuess}。你用了 ${attempts} 次尝试。`);
}
guessNumberGame();
3. C# (在控制台应用程序中运行): 猜数字游戏
csharp
using System;
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess)
{
Console.Write("猜一个1到100之间的数字: ");
if (!int.TryParse(Console.ReadLine(), out guess))
{
Console.WriteLine("请输入一个整数!");
continue;
}
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
}
Console.WriteLine($"恭喜你,猜对了!数字是 {numberToGuess}。你用了 {attempts} 次尝试。");
}
}
这些示例都是基于控制台的简单猜数字游戏。你可以根据自己的需要修改和扩展它们。