Python
python
import random
def guess_number(): bjgdnet.cn
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和JavaScript)
HTML部分 (index.html):
html
<script src="game.js"></script>
JavaScript部分 (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function guessNumber() {
const guessInput = document.getElementById(‘guessInput’);
const feedback = document.getElementById(‘feedback’);
const guess = parseInt(guessInput.value);
if (isNaN(guess) || guess < 1 || guess > 100) {
feedback.textContent = '请输入一个1到100之间的数字!';
return;
}
attempts++;
if (guess === numberToGuess) {
feedback.textContent = `恭喜你,你猜对了!用了${attempts}次尝试。`;
} else if (guess < numberToGuess) {
feedback.textContent = '太小了!';
} else {
feedback.textContent = '太大了!';
}
guessInput.value = ''; // 清空输入框
}
Java (使用Swing库)
Java代码相对较长,但这里是一个简化的框架:
java
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GuessNumberGame extends JFrame {
private JTextField guessInput;
private JLabel feedback;
private int numberToGuess;
private int attempts = 0;
// ... 构造函数、其他方法和事件监听器 ...
private void checkGuess() {
int guess = Integer.parseInt(guessInput.getText());
attempts++;
if (guess === numberToGuess) {
feedback.setText("恭喜你,你猜对了!用了" + attempts + "次尝试。");
} else if (guess < numberToGuess) {
feedback.setText("太小了!");
} else {
feedback.setText("太大了!");
}
guessInput.setText(""); // 清空输入框
}
// ... 添加按钮、监听器等 ...
}
注意:Java Swing部分需要完整的实现,包括GUI组件的初始化、事件监听器的添加等。这里只是给出了一个框架和checkGuess方法的示例。由于篇幅限制,我将为您提供几种不同编程语言(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和JavaScript)
HTML部分 (index.html):
html
<script src="game.js"></script>
JavaScript部分 (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function guessNumber() {
const guessInput = document.getElementById(‘guessInput’);
const feedback = document.getElementById(‘feedback’);
const guess = parseInt(guessInput.value);
if (isNaN(guess) || guess < 1 || guess > 100) {
feedback.textContent = '请输入一个1到100之间的数字!';
return;
}
attempts++;
if (guess === numberToGuess) {
feedback.textContent = `恭喜你,你猜对了!用了${attempts}次尝试。`;
} else if (guess < numberToGuess) {
feedback.textContent = '太小了!';
} else {
feedback.textContent = '太大了!';
}
guessInput.value = ''; // 清空输入框
}
Java (使用Swing库)
Java代码相对较长,但这里是一个简化的框架:
java
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GuessNumberGame extends JFrame {
private JTextField guessInput;
private JLabel feedback;
private int numberToGuess;
private int attempts = 0;
// ... 构造函数、其他方法和事件监听器 ...
private void checkGuess() {
int guess = Integer.parseInt(guessInput.getText());
attempts++;
if (guess === numberToGuess) {
feedback.setText("恭喜你,你猜对了!用了" + attempts + "次尝试。");
} else if (guess < numberToGuess) {
feedback.setText("太小了!");
} else {
feedback.setText("太大了!");
}
guessInput.setText(""); // 清空输入框
}
// ... 添加按钮、监听器等 ...
}
注意:Java Swing部分需要完整的实现,包括GUI组件的初始化、事件监听器的添加等。这里只是给出了一个框架和checkGuess方法的示例。