C语言一个不错的学习例子~

#include  <time.h>
#include  <stdlib.h>
#include  <stdio.h>
main()
{
    int  magic, guess, counter = 0, ret;
    char reply;
    srand(time(NULL));    //用系统时间做为随机数种子
    do{
        counter = 0;
        magic = rand() % 100 + 1;
        do{
            printf("Please guess a magic number:");
            ret = scanf("%d", &guess);
            while (ret != 1)
            {
                while (getchar() != '\n');    //把缓冲区中的字符全部读走
                printf("请输入一个1~100的整数:");
                ret = scanf("%d", &guess);
            }
            counter++;
            if (guess > magic)
                printf("Wrong!Too big!\n");
            else if (guess < magic)
                printf("Wrong!Too small!\n");
            else
                printf("Right!\n");
        } while (guess!=magic && counter<10);
        printf("counter = %d\n", counter);
        printf("The magic is %d\n",magic);
        printf("Do you want to continue(Y/N or y/n)?");
        scanf(" %c", &reply);  //这里的scanf前面还要有个空格,把回车读走
    }while (reply=='Y' || reply=='y');
}

猜你喜欢

转载自www.cnblogs.com/starboy13/p/12573037.html