
Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100) 1000sheng.com
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()
JavaScript (在HTML中嵌入)
HTML:
html
<script src="script.js"></script>
JavaScript (script.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess;
let attempts = 0;
function guessNumber() {
guess = parseInt(document.getElementById(‘guessInput’).value);
attempts++;
if (guess === numberToGuess) {
document.getElementById('result').textContent = `恭喜你,你猜对了!你用了${attempts}次尝试。`;
} else if (guess < numberToGuess) {
document.getElementById('result').textContent = "太小了!";
} else {
document.getElementById('result').textContent = "太大了!";
}
// 清空输入框
document.getElementById('guessInput').value = "";
}
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);
do {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} while (guess != numberToGuess);
System.out.println("恭喜你,你猜对了!你用了" + attempts + "次尝试。");
}
}
这些代码示例都创建了一个简单的“猜数字”游戏,其中程序会随机选择一个1到100之间的数字,然后让用户尝试猜测它。由于篇幅限制,我将为您提供三种不同编程语言(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:
guess = int(input("猜一个1到100之间的数字: "))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
print(f"恭喜你,你猜对了!你用了{attempts}次尝试。")
guess_number()
JavaScript (在HTML中嵌入)
HTML:
html
<script src="script.js"></script>
JavaScript (script.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess;
let attempts = 0;
function guessNumber() {
guess = parseInt(document.getElementById(‘guessInput’).value);
attempts++;
if (guess === numberToGuess) {
document.getElementById('result').textContent = `恭喜你,你猜对了!你用了${attempts}次尝试。`;
} else if (guess < numberToGuess) {
document.getElementById('result').textContent = "太小了!";
} else {
document.getElementById('result').textContent = "太大了!";
}
// 清空输入框
document.getElementById('guessInput').value = "";
}
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);
do {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} while (guess != numberToGuess);
System.out.println("恭喜你,你猜对了!你用了" + attempts + "次尝试。");
}
}
这些代码示例都创建了一个简单的“猜数字”游戏,其中程序会随机选择一个1到100之间的数字,然后让用户尝试猜测它。
697

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



