#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int randomNumber = rand() % 100 + 1;
int guessedNumber;
int attempts = 0;
cout << "猜数字游戏\n" << endl;
do {
cout << "请猜一个数字(1-100):" << endl;
cin >> guessedNumber;
attempts++;
if (guessedNumber > randomNumber) {
cout << "猜大了!" << endl;
} else if (guessedNumber < randomNumber) {
cout << "猜小了!" << endl;
} else {
cout << "恭喜你,猜对了!你用了 " << attempts << " 次猜中了数字 " << randomNumber << "。" << endl;
break;
}
} while (guessedNumber != randomNumber);
return 0;
}
该游戏会生成一个1到100之间的随机数字,玩家需要在每次猜测后得到提示来进行尝试,直到猜中为止。