Python 示例:简单的猜数字游戏grsyzp.cn
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'恭喜你,猜对了!数字是 {number_to_guess},你一共猜了 {attempts} 次。')
guess_number()
JavaScript 示例:控制台中的猜数字游戏(Node.js环境)
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guessNumber() {
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer);
attempts++;
if (guess === numberToGuess) {
console.log(恭喜你,猜对了!数字是 ${numberToGuess},你一共猜了 ${attempts} 次。
);
rl.close();
} else if (guess < numberToGuess) {
console.log(‘太小了!’);
guessNumber();
} else {
console.log(‘太大了!’);
guessNumber();
}
});
}
guessNumber();
Java 示例:使用Scanner的简单猜数字游戏
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);
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.println("恭喜你,猜对了!数字是 " + numberToGuess + ",你一共猜了 " + attempts + " 次。");
scanner.close();
}
}
这些示例都是简单的猜数字游戏,您可以在相应的环境中运行它们。请注意,这些代码只是游戏逻辑的基本框架,您可以根据需要添加更多的功能和改进。由于编写一个完整的小程序游戏代码涉及很多细节和复杂性,这里我将为您提供一些简化版的游戏概念代码,分别用Python(一种通用的脚本语言)、JavaScript(常用于网页和Node.js环境)和Java(一种常用的面向对象编程语言)来编写。
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'恭喜你,猜对了!数字是 {number_to_guess},你一共猜了 {attempts} 次。')
guess_number()
JavaScript 示例:控制台中的猜数字游戏(Node.js环境)
javascript
const readline = require(‘readline’);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guessNumber() {
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer);
attempts++;
if (guess === numberToGuess) {
console.log(恭喜你,猜对了!数字是 ${numberToGuess},你一共猜了 ${attempts} 次。
);
rl.close();
} else if (guess < numberToGuess) {
console.log(‘太小了!’);
guessNumber();
} else {
console.log(‘太大了!’);
guessNumber();
}
});
}
guessNumber();
Java 示例:使用Scanner的简单猜数字游戏
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);
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.println("恭喜你,猜对了!数字是 " + numberToGuess + ",你一共猜了 " + attempts + " 次。");
scanner.close();
}
}
这些示例都是简单的猜数字游戏,您可以在相应的环境中运行它们。请注意,这些代码只是游戏逻辑的基本框架,您可以根据需要添加更多的功能和改进。