Python
python
import random
def guess_number(): henanhuahan.com
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}次尝试。')
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 (isNaN(guess)) {
console.log('请输入一个整数!');
guessNumber();
return;
}
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
} else {
console.log(`恭喜你,你猜对了!用了${attempts}次尝试。`);
rl.close();
}
guessNumber();
});
}
guessNumber();
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("恭喜你,你猜对了!用了" + attempts + "次尝试。");
}
}
这些示例代码都提供了一个简单的“猜数字”游戏,用户需要猜测一个1到100之间的随机数。每个示例都包含了基本的输入验证和循环逻辑,直到用户猜对为止。由于篇幅限制,我无法为每种电脑语言都提供一个完整的小程序游戏代码,但我可以为你提供几种流行编程语言的简单游戏示例代码框架。这些示例将基于“猜数字”游戏,这是一种简单的命令行游戏。
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}次尝试。')
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 (isNaN(guess)) {
console.log('请输入一个整数!');
guessNumber();
return;
}
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
} else {
console.log(`恭喜你,你猜对了!用了${attempts}次尝试。`);
rl.close();
}
guessNumber();
});
}
guessNumber();
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("恭喜你,你猜对了!用了" + attempts + "次尝试。");
}
}
这些示例代码都提供了一个简单的“猜数字”游戏,用户需要猜测一个1到100之间的随机数。每个示例都包含了基本的输入验证和循环逻辑,直到用户猜对为止。