Python
python
import random
def guess_number():
secret_number = random.randint(1, 100) grsyzp.cn
guess = None
attempts = 0
while guess != secret_number:
try:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < secret_number:
print('太小了!')
elif guess > secret_number:
print('太大了!')
except ValueError:
print('输入无效,请输入一个整数。')
print(f'恭喜你,你猜对了! 用了{attempts}次尝试。')
guess_number()
JavaScript (Node.js 或浏览器)
javascript
function guessNumber() {
let secretNumber = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== secretNumber) {
guess = parseInt(prompt('猜一个1到100之间的数字: '));
attempts++;
if (isNaN(guess)) {
alert('输入无效,请输入一个整数。');
continue;
}
if (guess < secretNumber) {
alert('太小了!');
} else if (guess > secretNumber) {
alert('太大了!');
}
}
alert(`恭喜你,你猜对了! 用了${attempts}次尝试。`);
}
guessNumber();
Java
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int secretNumber = rand.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (guess != secretNumber) {
System.out.print("猜一个1到100之间的数字: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < secretNumber) {
System.out.println("太小了!");
} else if (guess > secretNumber) {
System.out.println("太大了!");
}
} else {
System.out.println("输入无效,请输入一个整数。");
scanner.next(); // 消耗掉非整数的输入
}
}
System.out.printf("恭喜你,你猜对了! 用了%d次尝试。\n", attempts);
}
}
这些示例都是简单的猜数字游戏,每种语言都有其特定的输入/输出和异常处理方法。您可以根据自己的需要和环境进行调整。由于篇幅和复杂性限制,我将为您提供几种流行编程语言中的简单游戏示例:猜数字游戏。
Python
python
import random
def guess_number():
secret_number = random.randint(1, 100)
guess = None
attempts = 0
while guess != secret_number:
try:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < secret_number:
print('太小了!')
elif guess > secret_number:
print('太大了!')
except ValueError:
print('输入无效,请输入一个整数。')
print(f'恭喜你,你猜对了! 用了{attempts}次尝试。')
guess_number()
JavaScript (Node.js 或浏览器)
javascript
function guessNumber() {
let secretNumber = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== secretNumber) {
guess = parseInt(prompt('猜一个1到100之间的数字: '));
attempts++;
if (isNaN(guess)) {
alert('输入无效,请输入一个整数。');
continue;
}
if (guess < secretNumber) {
alert('太小了!');
} else if (guess > secretNumber) {
alert('太大了!');
}
}
alert(`恭喜你,你猜对了! 用了${attempts}次尝试。`);
}
guessNumber();
Java
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int secretNumber = rand.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (guess != secretNumber) {
System.out.print("猜一个1到100之间的数字: ");
if (scanner.hasNextInt()) {
guess = scanner.nextInt();
attempts++;
if (guess < secretNumber) {
System.out.println("太小了!");
} else if (guess > secretNumber) {
System.out.println("太大了!");
}
} else {
System.out.println("输入无效,请输入一个整数。");
scanner.next(); // 消耗掉非整数的输入
}
}
System.out.printf("恭喜你,你猜对了! 用了%d次尝试。\n", attempts);
}
}
这些示例都是简单的猜数字游戏,每种语言都有其特定的输入/输出和异常处理方法。您可以根据自己的需要和环境进行调整。