- Python
python
import random
def guess_number(): wxyrsy.com
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}次尝试。")
guess_number()
2. JavaScript (HTML 网页中)
HTML:
html
<script src="game.js"></script>
JavaScript (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guessInput = document.getElementById(‘guess’);
let feedback = document.getElementById(‘feedback’);
let attempts = 0;
function checkGuess() {
attempts++;
let guess = parseInt(guessInput.value);
if (isNaN(guess)) {
feedback.textContent = “请输入一个整数!”;
return;
}
if (guess < numberToGuess) {
feedback.textContent = “太小了!”;
} else if (guess > numberToGuess) {
feedback.textContent = “太大了!”;
} else {
feedback.textContent = 恭喜你,猜对了!你用了${attempts}次尝试。
;
guessInput.value = ‘’;
}
}
3. Java (控制台应用)
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;
Scanner scanner = new Scanner(System.in);
int guess;
int attempts = 0;
// ... (省略输入/判断逻辑)
System.out.println("恭喜你,猜对了!你用了" + attempts + "次尝试。");
}
}
4. C# (控制台应用)
csharp
using System;
class Program {
static void Main() {
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess;
int attempts = 0;
// ... (省略输入/判断逻辑)
Console.WriteLine("恭喜你,猜对了!你用了" + attempts + "次尝试。");
}
}
以上代码仅提供了游戏的基本框架和逻辑,您可以根据需要进行扩展和优化。由于篇幅限制,我将为您提供几种不同编程语言的小游戏示例代码,每个代码都实现了一个简单的“猜数字”游戏。
- 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}次尝试。")
guess_number()
2. JavaScript (HTML 网页中)
HTML:
html
<script src="game.js"></script>
JavaScript (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guessInput = document.getElementById(‘guess’);
let feedback = document.getElementById(‘feedback’);
let attempts = 0;
function checkGuess() {
attempts++;
let guess = parseInt(guessInput.value);
if (isNaN(guess)) {
feedback.textContent = “请输入一个整数!”;
return;
}
if (guess < numberToGuess) {
feedback.textContent = “太小了!”;
} else if (guess > numberToGuess) {
feedback.textContent = “太大了!”;
} else {
feedback.textContent = 恭喜你,猜对了!你用了${attempts}次尝试。
;
guessInput.value = ‘’;
}
}
3. Java (控制台应用)
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;
Scanner scanner = new Scanner(System.in);
int guess;
int attempts = 0;
// ... (省略输入/判断逻辑)
System.out.println("恭喜你,猜对了!你用了" + attempts + "次尝试。");
}
}
4. C# (控制台应用)
csharp
using System;
class Program {
static void Main() {
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess;
int attempts = 0;
// ... (省略输入/判断逻辑)
Console.WriteLine("恭喜你,猜对了!你用了" + attempts + "次尝试。");
}
}
以上代码仅提供了游戏的基本框架和逻辑,您可以根据需要进行扩展和优化。