- Python
python
import random
def guess_number(): haoqian168.com
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input("猜一个1到100之间的数字: "))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
print(f"恭喜你,你猜对了!用了{attempts}次尝试。")
guess_number()
2. JavaScript (Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
readline.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
while (guess !== numberToGuess) {
attempts++;
if (guess < numberToGuess) {
console.log(‘太小了!’);
} else if (guess > numberToGuess) {
console.log(‘太大了!’);
}
readline.question('再试一次: ', (input) => {
guess = parseInt(input);
});
}
console.log(恭喜你,你猜对了!用了${attempts}次尝试。
);
readline.close();
});
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;
while (guess != numberToGuess)
{
Console.Write("猜一个1到100之间的数字: ");
guess = Convert.ToInt32(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
}
Console.WriteLine($"恭喜你,你猜对了!用了{attempts}次尝试。");
}
}
这些代码示例都非常基础,但它们展示了如何使用不同编程语言编写一个简单的控制台游戏。你可以根据自己的需要进行扩展和修改。由于“多种电脑语言”的范围非常广泛,我将为你提供三个常见编程语言(Python、JavaScript 和 C#)的简单控制台游戏代码示例。这些示例将是一个简单的“猜数字”游戏。
- Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input("猜一个1到100之间的数字: "))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
print(f"恭喜你,你猜对了!用了{attempts}次尝试。")
guess_number()
2. JavaScript (Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
readline.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
while (guess !== numberToGuess) {
attempts++;
if (guess < numberToGuess) {
console.log(‘太小了!’);
} else if (guess > numberToGuess) {
console.log(‘太大了!’);
}
readline.question('再试一次: ', (input) => {
guess = parseInt(input);
});
}
console.log(恭喜你,你猜对了!用了${attempts}次尝试。
);
readline.close();
});
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;
while (guess != numberToGuess)
{
Console.Write("猜一个1到100之间的数字: ");
guess = Convert.ToInt32(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
}
Console.WriteLine($"恭喜你,你猜对了!用了{attempts}次尝试。");
}
}
这些代码示例都非常基础,但它们展示了如何使用不同编程语言编写一个简单的控制台游戏。你可以根据自己的需要进行扩展和修改。