用c++写 猜数字 小游戏

#include<iostream>

using namespace std;

 

int main()

{

    cout << "=========猜数字========" << endl;

    cout << "规则:输入0~100的整数,有5次机会\n" << endl;

 

    // 以当前时间为随机数种子,生成一个0~100的伪随机数

    srand(time(0));    

    int target = rand() % 100;

 

    int n = 0;    // 猜的次数

    while (n < 5)

    {

         cout << "请输入0~100的整数:" << endl;

         int num;

         cin >> num;

 

         if (num == target)

         {

             cout << "恭喜你,猜对了! 幸运数字是:" << target << endl;

             break;

         }

         else if (num > target)

             cout << "数字太大了!再猜一遍!" << endl;

         else

             cout << "数字太小了!再猜一遍!" << endl;

 

         ++n;

    }

 

    if (n == 5)

         cout << "已经猜过5遍,没有猜中!欢迎下次再来!" << endl;

 

    cin.get();

    cin.get();

}

posted on 2023-09-12 14:28  songsonglailou  阅读(449)  评论(0)    收藏  举报

导航