
- Python - 猜数字游戏
python
import random
def guess_number():
number_to_guess = random.randint(1, 100) fengbaoshun.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()
2. JavaScript - 简单的HTML页面上的点击计数器
HTML:
html
点击次数: 0
<script src="counter.js"></script>
JavaScript (counter.js):
javascript
document.getElementById(‘counterBtn’).addEventListener(‘click’, function() {
let count = parseInt(document.getElementById(‘counterText’).textContent.split(': ')[1]) || 0;
count++;
document.getElementById(‘counterText’).textContent = '点击次数: ’ + count;
});
3. C++ - 控制台版的“石头、剪刀、布”游戏
cpp
#include
#include
#include
#include
std::string getComputerChoice() {
std::srand(static_cast(std::time(0)));
int choice = std::rand() % 3;
switch (choice) {
case 0: return “石头”;
case 1: return “剪刀”;
case 2: return “布”;
}
}
std::string getUserChoice() {
std::string choice;
std::cout << "请输入你的选择(石头/剪刀/布): ";
std::cin >> choice;
return choice;
}
std::string determineWinner(const std::string& userChoice, const std::string& computerChoice) {
if (userChoice == computerChoice) return “平局”;
if ((userChoice == “石头” && computerChoice == “剪刀”) ||
(userChoice == “剪刀” && computerChoice == “布”) ||
(userChoice == “布” && computerChoice == “石头”)) {
return “你赢了!”;
}
return “电脑赢了!”;
}
int main() {
std::string userChoice = getUserChoice();
std::string computerChoice = getComputerChoice();
std::cout << "你选择了: " << userChoice << std::endl;
std::cout << "电脑选择了: " << computerChoice << std::endl;
std::cout << determineWinner(userChoice, computerChoice) << std::endl;
return 0;
}
这些示例应该足够简单,以便你可以理解它们是如何工作的,并且可以根据需要进行修改和扩展。由于篇幅限制,我无法为所有编程语言都提供一个完整的小程序游戏代码,但我可以为你提供几个简单游戏的伪代码或示例代码片段,分别用Python、JavaScript和C++来表示。
- 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页面上的点击计数器
HTML:
html
点击次数: 0
<script src="counter.js"></script>
JavaScript (counter.js):
javascript
document.getElementById(‘counterBtn’).addEventListener(‘click’, function() {
let count = parseInt(document.getElementById(‘counterText’).textContent.split(': ')[1]) || 0;
count++;
document.getElementById(‘counterText’).textContent = '点击次数: ’ + count;
});
3. C++ - 控制台版的“石头、剪刀、布”游戏
cpp
#include
#include
#include
#include
std::string getComputerChoice() {
std::srand(static_cast(std::time(0)));
int choice = std::rand() % 3;
switch (choice) {
case 0: return “石头”;
case 1: return “剪刀”;
case 2: return “布”;
}
}
std::string getUserChoice() {
std::string choice;
std::cout << "请输入你的选择(石头/剪刀/布): ";
std::cin >> choice;
return choice;
}
std::string determineWinner(const std::string& userChoice, const std::string& computerChoice) {
if (userChoice == computerChoice) return “平局”;
if ((userChoice == “石头” && computerChoice == “剪刀”) ||
(userChoice == “剪刀” && computerChoice == “布”) ||
(userChoice == “布” && computerChoice == “石头”)) {
return “你赢了!”;
}
return “电脑赢了!”;
}
int main() {
std::string userChoice = getUserChoice();
std::string computerChoice = getComputerChoice();
std::cout << "你选择了: " << userChoice << std::endl;
std::cout << "电脑选择了: " << computerChoice << std::endl;
std::cout << determineWinner(userChoice, computerChoice) << std::endl;
return 0;
}
这些示例应该足够简单,以便你可以理解它们是如何工作的,并且可以根据需要进行修改和扩展。

1万+




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



