C程序-超简单的猜数字-游戏

游戏规则:随机产生一个100以内的整数,然后去猜这个数字,输入的数字大了,就提示应该输入小一点,反之输入的数字小了,就提示应该输入大一点,猜对了就结束游戏。

#include <iostream>
#include <time.h>
#include<stdlib.h>
using namespace std;

int main()
{
    srand((unsigned)time(NULL));//随机数种子
    int num = rand() % 100;//产生100以内的随机数

    int key;

    cout << "Please enter a number less than 100 : " << endl;

    while(1)
    {
        cin >> key;

        if(key > num)//输入的数大于目标数,应该输入小一点
        {
            cout << "The number should be smaller" << endl;
        }
        else if(key < num)//输入的数小于目标数,应该输入大一点
        {
            cout << "The number should be  bigger" << endl;
        }
        else
        {
            cout << "Bingou!" << endl;//猜对了
            break;
        }
    }
    
    return 0;
}

おすすめ

転載: blog.csdn.net/qq_40788199/article/details/102790839