
- Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100) 91386.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()
2. JavaScript (浏览器环境)
html
猜一个1到100之间的数字:
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
let guess = parseInt(document.getElementById('guessInput').value);
attempts++;
if (guess === numberToGuess) {
alert(`恭喜你,猜对了!你用了${attempts}次尝试。`);
resetGame();
} else if (guess < numberToGuess) {
alert('太小了!');
} else {
alert('太大了!');
}
}
function resetGame() {
numberToGuess = Math.floor(Math.random() * 100) + 1;
attempts = 0;
document.getElementById('message').textContent = '猜一个1到100之间的数字:';
document.getElementById('guessInput').value = '';
}
</script>
3. Java (控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.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 + "次尝试。");
}
}
以上代码分别用Python、JavaScript和Java实现了简单的猜数字游戏。每个程序都会随机生成一个1到100之间的数字,并让用户尝试猜测它。由于篇幅限制,我无法为所有电脑语言都提供一个完整的小程序游戏代码,但我可以为你提供几个常见编程语言的简单游戏代码示例:猜数字游戏。
- 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 (浏览器环境)
html
猜一个1到100之间的数字:
提交<script>
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
let guess = parseInt(document.getElementById('guessInput').value);
attempts++;
if (guess === numberToGuess) {
alert(`恭喜你,猜对了!你用了${attempts}次尝试。`);
resetGame();
} else if (guess < numberToGuess) {
alert('太小了!');
} else {
alert('太大了!');
}
}
function resetGame() {
numberToGuess = Math.floor(Math.random() * 100) + 1;
attempts = 0;
document.getElementById('message').textContent = '猜一个1到100之间的数字:';
document.getElementById('guessInput').value = '';
}
</script>
3. Java (控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.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 + "次尝试。");
}
}
以上代码分别用Python、JavaScript和Java实现了简单的猜数字游戏。每个程序都会随机生成一个1到100之间的数字,并让用户尝试猜测它。

被折叠的 条评论
为什么被折叠?



