Python 版本sidaotech.com
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
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("请输入一个有效的数字!")
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 guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来到猜数字游戏!");
System.out.println("我已经想好了一个1到100之间的数字。");
while (guess != numberToGuess) {
System.out.print("请输入你猜的数字:");
try {
guess = Integer.parseInt(scanner.nextLine());
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println(String.format("恭喜!你猜对了!数字就是%d。", numberToGuess));
System.out.println(String.format("你总共尝试了%d次。", attempts));
}
} catch (NumberFormatException e) {
System.out.println("请输入一个有效的数字!");
}
}
scanner.close();
}
}
C# 版本
csharp
using System;
class GuessNumberGame
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
Console.WriteLine("欢迎来到猜数字游戏!");
Console.WriteLine("我已经想好了一个1到100之间的数字。");
while (guess != numberToGuess)
{
Console.Write("请输入你猜的数字:");
try
{
guess = int.Parse(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
else
{
Console.WriteLine($"恭喜!你猜对了!数字就是{numberToGuess}。");
Console.WriteLine($"你总共尝试了{attempts}次。");
}
}
catch (FormatException)
{
Console.WriteLine("请输入一个有效的数字!");
}
}
}
}
这三个示例都实现了“猜数字”游戏,但是使用了不同的编程语言。每个示例都包含生成一个随机数作为要猜的数字、获取玩家输入、比较玩家猜测与实际数字、以及输出结果的逻辑。在玩家输入无效数字时,它们也都提供了错误处理。当然,我会为你提供几种不同编程语言实现的一个简单小游戏——“猜数字”的示例代码。这个游戏涉及计算机随机选择一个数字,玩家尝试猜测这个数字,直到猜中为止。
Python 版本
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
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("请输入一个有效的数字!")
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 guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎来到猜数字游戏!");
System.out.println("我已经想好了一个1到100之间的数字。");
while (guess != numberToGuess) {
System.out.print("请输入你猜的数字:");
try {
guess = Integer.parseInt(scanner.nextLine());
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println(String.format("恭喜!你猜对了!数字就是%d。", numberToGuess));
System.out.println(String.format("你总共尝试了%d次。", attempts));
}
} catch (NumberFormatException e) {
System.out.println("请输入一个有效的数字!");
}
}
scanner.close();
}
}
C# 版本
csharp
using System;
class GuessNumberGame
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
Console.WriteLine("欢迎来到猜数字游戏!");
Console.WriteLine("我已经想好了一个1到100之间的数字。");
while (guess != numberToGuess)
{
Console.Write("请输入你猜的数字:");
try
{
guess = int.Parse(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
else
{
Console.WriteLine($"恭喜!你猜对了!数字就是{numberToGuess}。");
Console.WriteLine($"你总共尝试了{attempts}次。");
}
}
catch (FormatException)
{
Console.WriteLine("请输入一个有效的数字!");
}
}
}
}
这三个示例都实现了“猜数字”游戏,但是使用了不同的编程语言。每个示例都包含生成一个随机数作为要猜的数字、获取玩家输入、比较玩家猜测与实际数字、以及输出结果的逻辑。在玩家输入无效数字时,它们也都提供了错误处理。