C++ 猜数字游戏代码:简单易懂,助你快速上手
#include
using namespace std;
int main() { int secretNum, guessNum, tryCount = 0; srand(time(0)); // 随机数种子 secretNum = rand() % 100 + 1; // 生成1-100之间的随机数
cout << '猜数字游戏开始!' << endl;
do {
cout << '请输入你猜测的数字(1-100之间):';
cin >> guessNum;
tryCount++;
if (guessNum > secretNum) {
cout << '你猜的数字太大了!' << endl;
} else if (guessNum < secretNum) {
cout << '你猜的数字太小了!' << endl;
} else {
cout << '恭喜你猜对了!' << endl;
break;
}
} while (true);
cout << '你总共猜了' << tryCount << '次。' << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/oZH4 著作权归作者所有。请勿转载和采集!