#include #include #include

using namespace std;

int main() { srand(time(NULL)); //设置随机数种子

int answer = rand() % 100 + 1; //生成1~100的随机数
int guess = 0;
int count = 0;

cout << "猜数字游戏开始!" << endl;

while (guess != answer)
{
    cout << "请输入你猜的数字(1~100):";
    cin >> guess;

    if (guess < answer)
    {
        cout << "猜小了!" << endl;
    }
    else if (guess > answer)
    {
        cout << "猜大了!" << endl;
    }

    count++;
}

cout << "恭喜你猜对了!" << endl;
cout << "你总共猜了" << count << "次。" << endl;

return 0;
使用C++写一段猜数字代码

原文地址: https://www.cveoy.top/t/topic/hfuZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录