由于篇幅限制,我无法为所有编程语言都提供一个完整的小程序游戏代码。不过,我可以分别为你展示三个流行编程语言的简单示例: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:
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'恭喜你,猜对了!数字是 {number_to_guess},你尝试了 {attempts} 次。')
guess_number()
JavaScript (在浏览器控制台或Node.js中运行)
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
guess = parseInt(prompt('猜一个1到100之间的数字: '));
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
}
}
console.log(`恭喜你,猜对了!数字是 ${numberToGuess},你尝试了 ${attempts} 次。`);
}
guessNumber();
C++
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
std::srand(static_cast<unsigned int>(std::time(nullptr)));
int numberToGuess = std::rand() % 100 + 1;
int guess;
int attempts = 0;
do {
std::cout << "猜一个1到100之间的数字: ";
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太小了!" << std::endl;
} else if (guess > numberToGuess) {
std::cout << "太大了!" << std::endl;
}
} while (guess != numberToGuess);
std::cout << "恭喜你,猜对了!数字是 " << numberToGuess << ",你尝试了 " << attempts << " 次。" << std::endl;
return 0;
}
以上三个示例都是“猜数字”游戏的简单实现,你可以根据自己的需要进行修改和扩展。由于篇幅限制,我无法为所有编程语言都提供一个完整的小程序游戏代码。不过,我可以分别为你展示三个流行编程语言的简单示例:Python、JavaScript 和 C++。这些示例都将是一个简单的“猜数字”游戏。
Python
python
import random
21cnnet.cn
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'恭喜你,猜对了!数字是 {number_to_guess},你尝试了 {attempts} 次。')
guess_number()
JavaScript (在浏览器控制台或Node.js中运行)
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
guess = parseInt(prompt('猜一个1到100之间的数字: '));
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
}
}
console.log(`恭喜你,猜对了!数字是 ${numberToGuess},你尝试了 ${attempts} 次。`);
}
guessNumber();
C++
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
std::srand(static_cast<unsigned int>(std::time(nullptr)));
int numberToGuess = std::rand() % 100 + 1;
int guess;
int attempts = 0;
do {
std::cout << "猜一个1到100之间的数字: ";
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太小了!" << std::endl;
} else if (guess > numberToGuess) {
std::cout << "太大了!" << std::endl;
}
} while (guess != numberToGuess);
std::cout << "恭喜你,猜对了!数字是 " << numberToGuess << ",你尝试了 " << attempts << " 次。" << std::endl;
return 0;
}
以上三个示例都是“猜数字”游戏的简单实现,你可以根据自己的需要进行修改和扩展。
1万+



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



