Python
python
import random
def game(): gzjiazheng.cn
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}次尝试。')
if name == “main”:
game()
JavaScript (Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function game() {
readline.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
attempts++;
if (guess === numberToGuess) {
console.log(恭喜你,猜对了! 用了${attempts}次尝试。
);
readline.close();
} else if (guess < numberToGuess) {
console.log(‘太小了!’);
game();
} else {
console.log(‘太大了!’);
game();
}
});
}
game();
Java
Java 的示例会稍微长一些,因为它需要处理更多的细节(比如输入/输出)。这里只提供一个简化的框架。
java
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.printf("恭喜你,猜对了! 用了%d次尝试。\n", attempts);
}
}
C#
csharp
using System;
class Program
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess)
{
Console.Write("猜一个1到100之间的数字: ");
guess = int.Parse(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
}
Console.WriteLine($"恭喜你,猜对了! 用了{attempts}次尝试。");
}
}
这些示例都是基于命令行界面的简单猜数字游戏。你可以根据自己的需要来扩展和修改它们。由于篇幅限制,我无法为每个编程语言都提供一个完整的游戏代码,但我可以为几种流行的编程语言提供简单的游戏示例代码,这些示例通常是一个简单的命令行游戏,比如猜数字游戏。
Python
python
import random
def game():
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}次尝试。')
if name == “main”:
game()
JavaScript (Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function game() {
readline.question('猜一个1到100之间的数字: ', (input) => {
guess = parseInt(input);
attempts++;
if (guess === numberToGuess) {
console.log(恭喜你,猜对了! 用了${attempts}次尝试。
);
readline.close();
} else if (guess < numberToGuess) {
console.log(‘太小了!’);
game();
} else {
console.log(‘太大了!’);
game();
}
});
}
game();
Java
Java 的示例会稍微长一些,因为它需要处理更多的细节(比如输入/输出)。这里只提供一个简化的框架。
java
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.printf("恭喜你,猜对了! 用了%d次尝试。\n", attempts);
}
}
C#
csharp
using System;
class Program
{
static void Main()
{
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
while (guess != numberToGuess)
{
Console.Write("猜一个1到100之间的数字: ");
guess = int.Parse(Console.ReadLine());
attempts++;
if (guess < numberToGuess)
{
Console.WriteLine("太小了!");
}
else if (guess > numberToGuess)
{
Console.WriteLine("太大了!");
}
}
Console.WriteLine($"恭喜你,猜对了! 用了{attempts}次尝试。");
}
}
这些示例都是基于命令行界面的简单猜数字游戏。你可以根据自己的需要来扩展和修改它们。