猜数游戏,随机目标数字,直到猜中退出

/* 猜数游戏(其三:目标数字是0~999的随机数)*/

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int no;        /* 读取的值 */
    int ans;    /* 目标数字 */

    srand(time(NULL));        /* 设定随机数的种子 */
    ans = rand() % 1000;    /* 生成0~999的随机数 */

    printf("请猜一个0~999的整数。

");

    do {
        printf("是多少呢:");
        scanf("%d", &no);

        if (no > ans)
            printf("a再小一点。
");
        else if (no < ans)
            printf("a再大一点。
");
    } while (no != ans);                    /* 重复到猜对为止 */

    printf("回答正确。
");

    return 0;
}

输出

请猜一个0~999的整数。

是多少呢:78
再大一点。
是多少呢:500
再小一点。
是多少呢:250
再小一点。
是多少呢:125
再小一点。
是多少呢:60
再大一点。
是多少呢:100
再小一点。
是多少呢:80
回答正确。
原文地址:https://www.cnblogs.com/sea-stream/p/11037442.html