Python
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100) aladingroup.cn
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_game()
JavaScript (浏览器环境)
html
<script>
function startGame() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
let output = document.getElementById('output');
function checkGuess() {
guess = prompt('猜一个1到100之间的数字:');
guess = parseInt(guess);
attempts++;
if (guess < numberToGuess) {
output.textContent = '太小了!';
} else if (guess > numberToGuess) {
output.textContent = '太大了!';
} else {
output.textContent = `恭喜你,猜对了!你用了${attempts}次尝试。`;
}
if (guess !== numberToGuess) {
setTimeout(checkGuess, 500); // 延时重新询问
}
}
checkGuess();
}
</script>
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之间的数字: ");
try {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} catch (Exception e) {
System.out.println("输入无效,请输入一个整数。");
scanner.next(); // 消耗掉错误的输入
}
}
System.out.println("恭喜你,猜对了!你用了" + attempts + "次尝试。");
}
}
请注意,这些代码示例是为了演示目的而简化的,并没有包含所有的错误处理和用户友好性特性。在开发实际的游戏或应用时,你可能需要添加更多的功能和细节。由于篇幅限制,我无法为每种编程语言都提供一个完整的游戏代码示例,但我可以为你提供几种常见编程语言中的简单小游戏(例如猜数字游戏)的伪代码或简化代码。
Python
python
import random
def guess_number_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'恭喜你,猜对了!你用了{attempts}次尝试。')
guess_number_game()
JavaScript (浏览器环境)
html
<script>
function startGame() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
let output = document.getElementById('output');
function checkGuess() {
guess = prompt('猜一个1到100之间的数字:');
guess = parseInt(guess);
attempts++;
if (guess < numberToGuess) {
output.textContent = '太小了!';
} else if (guess > numberToGuess) {
output.textContent = '太大了!';
} else {
output.textContent = `恭喜你,猜对了!你用了${attempts}次尝试。`;
}
if (guess !== numberToGuess) {
setTimeout(checkGuess, 500); // 延时重新询问
}
}
checkGuess();
}
</script>
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之间的数字: ");
try {
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} catch (Exception e) {
System.out.println("输入无效,请输入一个整数。");
scanner.next(); // 消耗掉错误的输入
}
}
System.out.println("恭喜你,猜对了!你用了" + attempts + "次尝试。");
}
}
请注意,这些代码示例是为了演示目的而简化的,并没有包含所有的错误处理和用户友好性特性。在开发实际的游戏或应用时,你可能需要添加更多的功能和细节。