Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100) zgshgys3w.com
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input("猜一个1到100之间的数字: "))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
except ValueError:
print("请输入一个整数!")
print(f"恭喜你,猜对了! 你一共猜了 {attempts} 次。")
if name == “main”:
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 startGame() {
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer, 10);
if (isNaN(guess)) {
console.log('请输入一个整数!');
startGame();
return;
}
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
startGame();
} else if (guess > numberToGuess) {
console.log('太大了!');
startGame();
} else {
console.log(`恭喜你,猜对了! 你一共猜了 ${attempts} 次。`);
rl.close();
}
});
}
startGame();
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.printf("恭喜你,猜对了! 你一共猜了 %d 次。\n", attempts);
}
}
以上三个示例都是简单的猜数字游戏,其中程序会随机生成一个1到100之间的数字,然后用户需要猜这个数字是多少。每次猜测后,程序会告诉用户他们的猜测是太大、太小还是正确。由于直接在这里展示多种电脑语言的完整游戏代码可能过于冗长,我将为你提供几种流行编程语言(Python、JavaScript、Java)的简单控制台游戏(猜数字游戏)的代码示例。
Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input("猜一个1到100之间的数字: "))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
except ValueError:
print("请输入一个整数!")
print(f"恭喜你,猜对了! 你一共猜了 {attempts} 次。")
if name == “main”:
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 startGame() {
rl.question('猜一个1到100之间的数字: ', (answer) => {
guess = parseInt(answer, 10);
if (isNaN(guess)) {
console.log('请输入一个整数!');
startGame();
return;
}
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
startGame();
} else if (guess > numberToGuess) {
console.log('太大了!');
startGame();
} else {
console.log(`恭喜你,猜对了! 你一共猜了 ${attempts} 次。`);
rl.close();
}
});
}
startGame();
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.printf("恭喜你,猜对了! 你一共猜了 %d 次。\n", attempts);
}
}
以上三个示例都是简单的猜数字游戏,其中程序会随机生成一个1到100之间的数字,然后用户需要猜这个数字是多少。每次猜测后,程序会告诉用户他们的猜测是太大、太小还是正确。