Python
python
import random
def guess_number(): gzjiazheng.cn
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} 次尝试。')
if name == “main”:
guess_number()
JavaScript (HTML 中嵌入)
html
猜数字游戏
猜一个 1 到 100 之间的数字:
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
const guess = parseInt(document.getElementById('guess').value);
attempts++;
if (guess === numberToGuess) {
document.getElementById('message').textContent = `恭喜你,你猜对了!你用了 ${attempts} 次尝试。`;
} else if (guess < numberToGuess) {
document.getElementById('message').textContent = '太低了!';
} else {
document.getElementById('message').textContent = '太高了!';
}
}
</script>
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;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("猜一个 1 到 100 之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess == numberToGuess) {
System.out.println("恭喜你,你猜对了!你用了 " + attempts + " 次尝试。");
break;
} else if (guess < numberToGuess) {
System.out.println("太低了!");
} else {
System.out.println("太高了!");
}
}
scanner.close();
}
}
以上代码分别展示了如何用 Python、JavaScript 和 Java 编写一个简单的“猜数字”游戏。由于篇幅限制,我不能为每种编程语言都提供一个完整的小程序游戏代码,但我可以为你提供几种常见编程语言(如 Python、JavaScript、Java)的简单小游戏示例代码。这些示例通常是一个“猜数字”游戏。
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} 次尝试。')
if name == “main”:
guess_number()
JavaScript (HTML 中嵌入)
html
猜数字游戏
猜一个 1 到 100 之间的数字:
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
const guess = parseInt(document.getElementById('guess').value);
attempts++;
if (guess === numberToGuess) {
document.getElementById('message').textContent = `恭喜你,你猜对了!你用了 ${attempts} 次尝试。`;
} else if (guess < numberToGuess) {
document.getElementById('message').textContent = '太低了!';
} else {
document.getElementById('message').textContent = '太高了!';
}
}
</script>
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;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("猜一个 1 到 100 之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess == numberToGuess) {
System.out.println("恭喜你,你猜对了!你用了 " + attempts + " 次尝试。");
break;
} else if (guess < numberToGuess) {
System.out.println("太低了!");
} else {
System.out.println("太高了!");
}
}
scanner.close();
}
}
以上代码分别展示了如何用 Python、JavaScript 和 Java 编写一个简单的“猜数字”游戏。