- Python 版本的猜数字游戏
python
import random
def guess_number_game(): aizhuw.cn
number = random.randint(1, 100)
attempts = 0
while True:
user_guess = int(input("请猜一个1到100之间的数字: “))
attempts += 1
if user_guess == number:
print(f"恭喜你,你猜对了!你用了{attempts}次尝试。”)
break
elif user_guess < number:
print(“太小了,请再试一次。”)
else:
print(“太大了,请再试一次。”)
guess_number_game()
2. Java 版本的猜数字游戏
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int number = rand.nextInt(100) + 1;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请猜一个1到100之间的数字: ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == number) {
System.out.println(“恭喜你,你猜对了!你用了” + attempts + “次尝试。”);
break;
} else if (userGuess < number) {
System.out.println(“太小了,请再试一次。”);
} else {
System.out.println(“太大了,请再试一次。”);
}
}
scanner.close();
}
}
3. C++ 版本的猜数字游戏
cpp
#include
#include
#include
using namespace std;
int main() {
srand(time(0)); // 初始化随机数生成器
int number = rand() % 100 + 1; // 生成1到100的随机数
int attempts = 0;
int userGuess;
while (true) {
cout << "请猜一个1到100之间的数字: ";
cin >> userGuess;
attempts++;
if (userGuess == number) {
cout << “恭喜你,你猜对了!你用了” << attempts << “次尝试。” << endl;
break;
} else if (userGuess < number) {
cout << “太小了,请再试一次。” << endl;
} else {
cout << “太大了,请再试一次。” << endl;
}
}
return 0;
}
以上三个程序都是简单的猜数字游戏,程序会随机生成一个1到100之间的数字,然后让用户猜测。根据用户的猜测,程序会给出提示,直到用户猜对为止。当然可以!由于篇幅限制,我会为你提供三种不同编程语言的简单控制台游戏代码:Python、Java和C++。
- Python 版本的猜数字游戏
python
import random
def guess_number_game():
number = random.randint(1, 100)
attempts = 0
while True:
user_guess = int(input("请猜一个1到100之间的数字: “))
attempts += 1
if user_guess == number:
print(f"恭喜你,你猜对了!你用了{attempts}次尝试。”)
break
elif user_guess < number:
print(“太小了,请再试一次。”)
else:
print(“太大了,请再试一次。”)
guess_number_game()
2. Java 版本的猜数字游戏
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int number = rand.nextInt(100) + 1;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请猜一个1到100之间的数字: ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == number) {
System.out.println(“恭喜你,你猜对了!你用了” + attempts + “次尝试。”);
break;
} else if (userGuess < number) {
System.out.println(“太小了,请再试一次。”);
} else {
System.out.println(“太大了,请再试一次。”);
}
}
scanner.close();
}
}
3. C++ 版本的猜数字游戏
cpp
#include
#include
#include
using namespace std;
int main() {
srand(time(0)); // 初始化随机数生成器
int number = rand() % 100 + 1; // 生成1到100的随机数
int attempts = 0;
int userGuess;
while (true) {
cout << "请猜一个1到100之间的数字: ";
cin >> userGuess;
attempts++;
if (userGuess == number) {
cout << “恭喜你,你猜对了!你用了” << attempts << “次尝试。” << endl;
break;
} else if (userGuess < number) {
cout << “太小了,请再试一次。” << endl;
} else {
cout << “太大了,请再试一次。” << endl;
}
}
return 0;
}
以上三个程序都是简单的猜数字游戏,程序会随机生成一个1到100之间的数字,然后让用户猜测。根据用户的猜测,程序会给出提示,直到用户猜对为止。