Python 示例:猜数字游戏
python
import random
def game():
number_to_guess = random.randint(1, 100) siguansheji.com
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} 次。')
if name == “main”:
game()
JavaScript 示例:控制台猜数字游戏(Node.js 环境)
javascript
// 在Node.js环境中运行
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 game() {
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer);
attempts++;
if (guess === numberToGuess) {
console.log(`恭喜你,猜对了!数字是 ${numberToGuess}。你尝试了 ${attempts} 次。`);
rl.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 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 + " 次。");
}
}
注意:Java 代码需要编译并运行(例如,使用 javac GuessNumberGame.java 编译,然后使用 java GuessNumberGame 运行)。而 JavaScript 示例代码需要在 Node.js 环境中运行。Python 代码则可以直接运行。由于不同电脑语言(或编程语言)的特性和用途各不相同,且编写完整的小程序游戏代码可能相当冗长,这里我将分别为你提供几种常见编程语言(Python、JavaScript、Java)的简单控制台游戏的代码示例。
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'恭喜你,猜对了!数字是 {number_to_guess}。你尝试了 {attempts} 次。')
if name == “main”:
game()
JavaScript 示例:控制台猜数字游戏(Node.js 环境)
javascript
// 在Node.js环境中运行
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 game() {
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer);
attempts++;
if (guess === numberToGuess) {
console.log(`恭喜你,猜对了!数字是 ${numberToGuess}。你尝试了 ${attempts} 次。`);
rl.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 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 + " 次。");
}
}
注意:Java 代码需要编译并运行(例如,使用 javac GuessNumberGame.java 编译,然后使用 java GuessNumberGame 运行)。而 JavaScript 示例代码需要在 Node.js 环境中运行。Python 代码则可以直接运行。