Python
python
import random
def game(): kykzl.com
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}次尝试。')
if name == “main”:
game()
JavaScript (HTML内嵌)
html
<script>
function startGame() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guessNumber() {
guess = parseInt(prompt('猜一个1到100之间的数字: '));
attempts++;
if (guess < numberToGuess) {
alert('太小了!');
} else if (guess > numberToGuess) {
alert('太大了!');
} else {
alert(`恭喜你,你猜对了!用了${attempts}次尝试。`);
document.querySelector('button').textContent = '重新开始';
}
if (guess !== numberToGuess) {
setTimeout(guessNumber, 500); // 延迟再次询问,增加用户体验
}
}
guessNumber();
}
</script>
Java java import java.util.Random; import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
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 + "次尝试。");
}
}
以上三个示例都是简单的猜数字游戏,你可以根据需求进行修改或扩展。由于篇幅限制,我不能为每种电脑语言都提供一个完整的小程序游戏代码。但我可以为你提供几种流行编程语言(如Python, JavaScript, 和Java)的简单游戏示例代码,这些代码实现了一个基本的猜数字游戏。
Python
python
import random
def 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}次尝试。')
if name == “main”:
game()
JavaScript (HTML内嵌)
html
<script>
function startGame() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function guessNumber() {
guess = parseInt(prompt('猜一个1到100之间的数字: '));
attempts++;
if (guess < numberToGuess) {
alert('太小了!');
} else if (guess > numberToGuess) {
alert('太大了!');
} else {
alert(`恭喜你,你猜对了!用了${attempts}次尝试。`);
document.querySelector('button').textContent = '重新开始';
}
if (guess !== numberToGuess) {
setTimeout(guessNumber, 500); // 延迟再次询问,增加用户体验
}
}
guessNumber();
}
</script>
Java java import java.util.Random; import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int attempts = 0;
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 + "次尝试。");
}
}
以上三个示例都是简单的猜数字游戏,你可以根据需求进行修改或扩展。