c++猜数字小游戏,话不多说,上代码:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
srand(time(0)); // 初始化随机数种子
int secretNumber = rand() % 100 + 1; // 生成1到100之间的随机数
int guess;
int attempts = 0;
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我已经想好了一个1到100之间的数字,你能猜到吗?" << endl;
do {
cout << "请输入你的猜测:";
cin >> guess;
attempts++;
if (guess > secretNumber) {
cout << "太大了!" << endl;
} else if (guess < secretNumber) {
cout << "太小了!" << endl;
} else {
cout << "恭喜你,猜对了!" << endl;
cout << "你一共猜了" << attempts << "次。" << endl;
}
} while (guess != secretNumber);
return 0;
}