c++小游戏
以下是一个简单的 C++ 小游戏示例,名为 "猜数字":
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0)); // 随机数种子
int secretNumber = rand() % 100 + 1; // 生成1到100的随机数
int guess;
int attempts = 0;
std::cout << "欢迎来到猜数字游戏!" << std::endl;
std::cout << "我已经想好了一个1到100之间的数字,你需要猜出来。" << std::endl;
do {
std::cout << "请输入你的猜测:";
std::cin >> guess;
attempts++;
if (guess > secretNumber) {
std::cout << "猜的数字太大了!" << std::endl;
} else if (guess < secretNumber) {
std::cout << "猜的数字太小了!" << std::endl;
} else {
std::cout << "恭喜你猜对了!" << std::endl;
std::cout << "你用了" << attempts << "次猜测。" << std::endl;
}
} while (guess != secretNumber);
return 0;
}
这个小游戏会生成一个1到100之间的随机数,然后让玩家通过输入猜测的数字来猜出这个随机数。游戏会根据玩家的猜测给出提示,直到玩家猜对为止。同时,游戏会统计玩家猜测的次数并在玩家猜对后显示出来。
注意,这只是一个简单的示例,你可以根据自己的需求对其进行修改和扩展
原文地址: https://www.cveoy.top/t/topic/iauo 著作权归作者所有。请勿转载和采集!