- Python
python
import random
def guess_number(): 1i8.cn
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
print("我已经想好了一个1到100之间的数字,你有无限次机会来猜它。")
while guess != number_to_guess:
guess = int(input("请猜一个数字:"))
attempts += 1
if guess < number_to_guess:
print("你猜的数字太小了!")
elif guess > number_to_guess:
print("你猜的数字太大了!")
print(f"恭喜你,猜对了!数字是 {number_to_guess}。你用了 {attempts} 次尝试。")
guess_number()
2. JavaScript (在浏览器控制台或Node.js环境中运行)
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
console.log("欢迎来到猜数字游戏!");
console.log("我已经想好了一个1到100之间的数字,你有无限次机会来猜它。");
while (guess !== numberToGuess) {
guess = parseInt(prompt("请猜一个数字:"));
attempts++;
if (guess < numberToGuess) {
console.log("你猜的数字太小了!");
} else if (guess > numberToGuess) {
console.log("你猜的数字太大了!");
}
}
console.log(`恭喜你,猜对了!数字是 ${numberToGuess}。你用了 ${attempts} 次尝试。`);
}
guessNumber();
3. C# (控制台应用程序)
csharp
using System;
class Program
{
static void Main()
{
Random rnd = new Random();
int numberToGuess = rnd.Next(1, 101);
int guess = 0;
int attempts = 0;
Console.WriteLine("欢迎来到猜数字游戏!");
Console.WriteLine("我已经想好了一个1到100之间的数字,你有无限次机会来猜它。");
do
{
Console.Write("请猜一个数字:");
guess = Convert.ToInt32(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("你猜的数字太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("你猜的数字太大了!");
}
} while (guess != numberToGuess);
Console.WriteLine($"恭喜你,猜对了!数字是 {numberToGuess}。你用了 {attempts} 次尝试。");
}
}
这些代码示例提供了一个简单的“猜数字”游戏,玩家需要猜测一个在1到100之间的随机数字。每个程序都会根据玩家的猜测给出提示,直到猜对为止。由于生成完整的小程序游戏代码可能会很长且复杂,我将为你提供几种不同编程语言(Python、JavaScript、C#)的简单“猜数字”游戏的基础代码示例。
- Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
print("我已经想好了一个1到100之间的数字,你有无限次机会来猜它。")
while guess != number_to_guess:
guess = int(input("请猜一个数字:"))
attempts += 1
if guess < number_to_guess:
print("你猜的数字太小了!")
elif guess > number_to_guess:
print("你猜的数字太大了!")
print(f"恭喜你,猜对了!数字是 {number_to_guess}。你用了 {attempts} 次尝试。")
guess_number()
2. JavaScript (在浏览器控制台或Node.js环境中运行)
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
console.log("欢迎来到猜数字游戏!");
console.log("我已经想好了一个1到100之间的数字,你有无限次机会来猜它。");
while (guess !== numberToGuess) {
guess = parseInt(prompt("请猜一个数字:"));
attempts++;
if (guess < numberToGuess) {
console.log("你猜的数字太小了!");
} else if (guess > numberToGuess) {
console.log("你猜的数字太大了!");
}
}
console.log(`恭喜你,猜对了!数字是 ${numberToGuess}。你用了 ${attempts} 次尝试。`);
}
guessNumber();
3. C# (控制台应用程序)
csharp
using System;
class Program
{
static void Main()
{
Random rnd = new Random();
int numberToGuess = rnd.Next(1, 101);
int guess = 0;
int attempts = 0;
Console.WriteLine("欢迎来到猜数字游戏!");
Console.WriteLine("我已经想好了一个1到100之间的数字,你有无限次机会来猜它。");
do
{
Console.Write("请猜一个数字:");
guess = Convert.ToInt32(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("你猜的数字太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("你猜的数字太大了!");
}
} while (guess != numberToGuess);
Console.WriteLine($"恭喜你,猜对了!数字是 {numberToGuess}。你用了 {attempts} 次尝试。");
}
}
这些代码示例提供了一个简单的“猜数字”游戏,玩家需要猜测一个在1到100之间的随机数字。每个程序都会根据玩家的猜测给出提示,直到猜对为止。