Python 版本
python
import random
def guess_number_game(): mmcml.com
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("请输入一个有效的数字!")
if name == “main”:
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("请输入你的猜测:");
if (scanner.hasNextInt()) {
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 + "次。");
}
} else {
System.out.println("请输入一个有效的数字!");
scanner.next(); // 清除输入
}
}
scanner.close();
}
}
C++ 版本
cpp
#include
#include
#include
int main() {
std::srand(unsigned(std::time(0))); // 用当前时间作为随机数种子
int numberToGuess = std::rand() % 100 + 1;
int guess = 0;
int attempts = 0;
std::cout << "欢迎来到猜数字游戏!\n";
std::cout << "我已经选了一个1到100之间的数字,你能猜到是哪个吗?\n";
while (guess != numberToGuess) {
std::cout << "请输入你的猜测:";
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太小了!\n";
} else if (guess > numberToGuess) {
std::cout << "太大了!\n";
} else {
std::cout << "恭喜!你猜对了!数字就是" << numberToGuess << "。\n";
std::cout << "你一共尝试了" << attempts << "次。\n";
}
}
return 0;
}
这三个版本的猜数字游戏都遵循相似的逻辑:随机选择一个数字,然后让玩家猜测这个数字,根据猜测给出提示,直到猜对为止。每个版本的语法和库使用有所不同,但核心逻辑是相似的。由于不同的编程语言有不同的特性和语法,我将为你提供一个简单的猜数字游戏示例,分别用Python、Java和C++来实现。
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("请输入一个有效的数字!")
if name == “main”:
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("请输入你的猜测:");
if (scanner.hasNextInt()) {
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 + "次。");
}
} else {
System.out.println("请输入一个有效的数字!");
scanner.next(); // 清除输入
}
}
scanner.close();
}
}
C++ 版本
cpp
#include
#include
#include
int main() {
std::srand(unsigned(std::time(0))); // 用当前时间作为随机数种子
int numberToGuess = std::rand() % 100 + 1;
int guess = 0;
int attempts = 0;
std::cout << "欢迎来到猜数字游戏!\n";
std::cout << "我已经选了一个1到100之间的数字,你能猜到是哪个吗?\n";
while (guess != numberToGuess) {
std::cout << "请输入你的猜测:";
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太小了!\n";
} else if (guess > numberToGuess) {
std::cout << "太大了!\n";
} else {
std::cout << "恭喜!你猜对了!数字就是" << numberToGuess << "。\n";
std::cout << "你一共尝试了" << attempts << "次。\n";
}
}
return 0;
}
这三个版本的猜数字游戏都遵循相似的逻辑:随机选择一个数字,然后让玩家猜测这个数字,根据猜测给出提示,直到猜对为止。每个版本的语法和库使用有所不同,但核心逻辑是相似的。