美文网首页
13.while循环

13.while循环

作者: lxr_ | 来源:发表于2020-12-13 12:33 被阅读0次

#include<iostream>

#include<ctime>//或者time.h,使用随机数种子时用到

using namespace std;

int main()

{

    //随机种子,保证程序每次执行时随机数都不一样

    srand((unsigned int)time(NULL));

    int ch = rand()%100+1;//1到100的随机数

    //cout << ch << endl;

    int num=0;

    cout << "请输入你猜的数" << endl;

    cin >> num;

    while (num != ch)

    {

        if (num > ch)

        {

            cout << "您输入的数字过大,请重新输入" << endl;

            cin >> num;

        }

        else

        {

            cout << "您输入的数字过小,请重新输入" << endl;

            cin >> num;

        }

    }

    cout << "恭喜:" <<ch<< endl;

    system("pause");

    return 0;

}

相关文章

网友评论

      本文标题:13.while循环

      本文链接:https://www.haomeiwen.com/subject/aajawktx.html