猜数游戏,不猜对就不退出

 

/*猜数游戏(其二:重复到猜对为止——利用do语句)*/

#include <stdio.h>

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

    printf("请猜一个0~9的整数。\n\n");

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

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

    printf("回答正确。\n");

    return 0;
}

输出

请猜一个0~9的整数。

是多少呢:9
再小一点。
是多少呢:7
回答正确。

 

posted @ 2019-06-16 23:27  anobscureretreat  阅读(108)  评论(0编辑  收藏  举报