Python 版本
python
import random
def guess_the_number(): yctsy.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}。你总共尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字!")
guess_the_number()
Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
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("我已经想好了一个1到100之间的数字。你能猜到是哪个吗?");
while (guess != numberToGuess) {
System.out.print("请输入你的猜测:");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println(String.format("恭喜你!猜对了!数字就是%d。你总共尝试了%d次。", numberToGuess, attempts));
}
} else {
System.out.println("请输入一个有效的数字!");
scanner.next(); // 消耗掉错误的输入
}
}
scanner.close();
}
}
C# 版本
csharp
using System;
class GuessTheNumber
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
Console.WriteLine("我已经想好了一个1到100之间的数字。你能猜到是哪个吗?");
while (guess != numberToGuess)
{
Console.Write("请输入你的猜测:");
if (int.TryParse(Console.ReadLine(), out guess))
{
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
else
{
Console.WriteLine($"恭喜你!猜对了!数字就是{numberToGuess}。你总共尝试了{attempts}次。");
}
}
else
{
Console.WriteLine("请输入一个有效的数字!");
}
}
}
}
这三个示例都实现了“猜数字”游戏,但是使用了不同的编程语言。每个示例都包含生成随机数、获取玩家输入、判断猜测结果以及循环直到猜对为止的逻辑。当然可以,下面我将为你提供几种不同编程语言实现的一个简单小游戏——“猜数字”的示例代码。这个游戏通常涉及计算机随机选择一个数字,然后玩家尝试猜测这个数字,直到猜对为止。
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}。你总共尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字!")
guess_the_number()
Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
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("我已经想好了一个1到100之间的数字。你能猜到是哪个吗?");
while (guess != numberToGuess) {
System.out.print("请输入你的猜测:");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
} else {
System.out.println(String.format("恭喜你!猜对了!数字就是%d。你总共尝试了%d次。", numberToGuess, attempts));
}
} else {
System.out.println("请输入一个有效的数字!");
scanner.next(); // 消耗掉错误的输入
}
}
scanner.close();
}
}
C# 版本
csharp
using System;
class GuessTheNumber
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
Console.WriteLine("我已经想好了一个1到100之间的数字。你能猜到是哪个吗?");
while (guess != numberToGuess)
{
Console.Write("请输入你的猜测:");
if (int.TryParse(Console.ReadLine(), out guess))
{
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
else
{
Console.WriteLine($"恭喜你!猜对了!数字就是{numberToGuess}。你总共尝试了{attempts}次。");
}
}
else
{
Console.WriteLine("请输入一个有效的数字!");
}
}
}
}
这三个示例都实现了“猜数字”游戏,但是使用了不同的编程语言。每个示例都包含生成随机数、获取玩家输入、判断猜测结果以及循环直到猜对为止的逻辑。