- Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100) ssvoip.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'恭喜你,你猜对了!用了{attempts}次尝试。')
guess_number()
2. JavaScript (Node.js 或浏览器环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
readline.question('猜一个1到100之间的数字: ', (answer) => {
while (true) {
guess = parseInt(answer, 10);
attempts++;
if (isNaN(guess) || guess < 1 || guess > 100) {
console.log(‘请输入一个1到100之间的整数!’);
readline.question('再试一次: ', (newAnswer) => {
answer = newAnswer;
});
continue;
}
if (guess < numberToGuess) {
console.log(‘太低了!’);
} else if (guess > numberToGuess) {
console.log(‘太高了!’);
} else {
console.log(恭喜你,你猜对了!用了${attempts}次尝试。
);
break;
}
readline.question('再猜一次: ', (newAnswer) => {
answer = newAnswer;
});
}
readline.close();
});
3. Java
Java 的控制台输入/输出相对复杂,但以下是一个简单的示例:
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
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 + "次尝试。");
scanner.close();
}
}
这些代码示例提供了“猜数字”游戏的基本框架,但你可以根据需要进行扩展和修改。由于直接在一个回答中展示多种语言的完整游戏代码可能过于冗长,我将为你提供几种不同编程语言中简单的“猜数字”游戏的基本框架。这个游戏会让电脑随机选择一个数字,然后让用户尝试猜测这个数字。
- 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'恭喜你,你猜对了!用了{attempts}次尝试。')
guess_number()
2. JavaScript (Node.js 或浏览器环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
readline.question('猜一个1到100之间的数字: ', (answer) => {
while (true) {
guess = parseInt(answer, 10);
attempts++;
if (isNaN(guess) || guess < 1 || guess > 100) {
console.log(‘请输入一个1到100之间的整数!’);
readline.question('再试一次: ', (newAnswer) => {
answer = newAnswer;
});
continue;
}
if (guess < numberToGuess) {
console.log(‘太低了!’);
} else if (guess > numberToGuess) {
console.log(‘太高了!’);
} else {
console.log(恭喜你,你猜对了!用了${attempts}次尝试。
);
break;
}
readline.question('再猜一次: ', (newAnswer) => {
answer = newAnswer;
});
}
readline.close();
});
3. Java
Java 的控制台输入/输出相对复杂,但以下是一个简单的示例:
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
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 + "次尝试。");
scanner.close();
}
}
这些代码示例提供了“猜数字”游戏的基本框架,但你可以根据需要进行扩展和修改。