Python 版本meimiaomeimo.cn
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
attempts = 0
print(“欢迎来到猜数字游戏!”)
print(“我已经想好了一个1到100之间的数字。”)
while True:
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}次。")
break
except ValueError:
print("请输入一个有效的数字。")
guess_number_game()
Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来到猜数字游戏!");
System.out.println("我已经想好了一个1到100之间的数字。");
while (true) {
System.out.print("请猜一个数字:");
if (scanner.hasNextInt()) {
int guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println("恭喜你!答对了!数字就是" + numberToGuess + "。");
System.out.println("你总共猜了" + attempts + "次。");
break;
}
} else {
System.out.println("请输入一个有效的数字。");
scanner.next(); // 消耗掉错误的输入
}
}
scanner.close();
}
}
C# 版本
csharp
using System;
class Program
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int attempts = 0;
Console.WriteLine("欢迎来到猜数字游戏!");
Console.WriteLine("我已经想好了一个1到100之间的数字。");
while (true)
{
Console.Write("请猜一个数字:");
if (int.TryParse(Console.ReadLine(), out int guess))
{
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
else
{
Console.WriteLine($"恭喜你!答对了!数字就是{numberToGuess}。");
Console.WriteLine($"你总共猜了{attempts}次。");
break;
}
}
else
{
Console.WriteLine("请输入一个有效的数字。");
}
}
}
}
这三个示例程序都实现了相同的“猜数字”游戏逻辑,但使用了不同的编程语言和库。由于篇幅限制,我将为你提供一个简单的“猜数字”游戏示例,分别用Python、Java(控制台应用程序)和C#(控制台应用程序)实现。这个游戏会随机生成一个1到100之间的数字,然后让玩家猜这个数字是多少,直到猜中为止。
Python 版本
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
attempts = 0
print(“欢迎来到猜数字游戏!”)
print(“我已经想好了一个1到100之间的数字。”)
while True:
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}次。")
break
except ValueError:
print("请输入一个有效的数字。")
guess_number_game()
Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来到猜数字游戏!");
System.out.println("我已经想好了一个1到100之间的数字。");
while (true) {
System.out.print("请猜一个数字:");
if (scanner.hasNextInt()) {
int guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println("恭喜你!答对了!数字就是" + numberToGuess + "。");
System.out.println("你总共猜了" + attempts + "次。");
break;
}
} else {
System.out.println("请输入一个有效的数字。");
scanner.next(); // 消耗掉错误的输入
}
}
scanner.close();
}
}
C# 版本
csharp
using System;
class Program
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int attempts = 0;
Console.WriteLine("欢迎来到猜数字游戏!");
Console.WriteLine("我已经想好了一个1到100之间的数字。");
while (true)
{
Console.Write("请猜一个数字:");
if (int.TryParse(Console.ReadLine(), out int guess))
{
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
else
{
Console.WriteLine($"恭喜你!答对了!数字就是{numberToGuess}。");
Console.WriteLine($"你总共猜了{attempts}次。");
break;
}
}
else
{
Console.WriteLine("请输入一个有效的数字。");
}
}
}
}
这三个示例程序都实现了相同的“猜数字”游戏逻辑,但使用了不同的编程语言和库。