- Python
python
import random
def game():
number_to_guess = random.randint(1, 100) maoxianjiu.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}次尝试。')
if name == “main”:
game()
2. JavaScript (HTML 页面中)
HTML:
html
猜数字游戏
猜<script src="game.js"></script>
JavaScript (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
let guess = parseInt(document.getElementById(‘guessInput’).value);
attempts++;
if (guess === numberToGuess) {
document.getElementById('feedback').textContent = `恭喜你,你猜对了!用了${attempts}次尝试。`;
} else if (guess < numberToGuess) {
document.getElementById('feedback').textContent = '太低了!';
} else {
document.getElementById('feedback').textContent = '太高了!';
}
}
3. Java (控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
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之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
}
}
System.out.printf("恭喜你,你猜对了!用了%d次尝试。\n", attempts);
}
}
这些示例都是简单的猜数字游戏,您可以根据自己的需要进行扩展或修改。由于篇幅限制,我将为您提供几种不同编程语言中简单的小游戏(例如猜数字游戏)的代码示例。
- 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()
2. JavaScript (HTML 页面中)
HTML:
html
猜数字游戏
猜<script src="game.js"></script>
JavaScript (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
let guess = parseInt(document.getElementById(‘guessInput’).value);
attempts++;
if (guess === numberToGuess) {
document.getElementById('feedback').textContent = `恭喜你,你猜对了!用了${attempts}次尝试。`;
} else if (guess < numberToGuess) {
document.getElementById('feedback').textContent = '太低了!';
} else {
document.getElementById('feedback').textContent = '太高了!';
}
}
3. Java (控制台应用)
java
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
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之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太低了!");
} else if (guess > numberToGuess) {
System.out.println("太高了!");
}
}
System.out.printf("恭喜你,你猜对了!用了%d次尝试。\n", attempts);
}
}
这些示例都是简单的猜数字游戏,您可以根据自己的需要进行扩展或修改。