A Simple Guess Number Game

2010/11/17

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

#define NO 0
#define YES 1

int main(void)
{
    int guess_value = -1;
    int number;
    int nbr_of_guesses;
    int done = NO;

    //获取随机数
    srand((unsigned)time(NULL));
    number = abs(rand()%100)+1;/*注意rand函数使用前要用到上面的srand那一行代码
                               产生一个1~100的随机数*/

    nbr_of_guesses = 0;
    while(done == NO)
    {
        printf("/nPick a number between 0 and %d/n",100);
        scanf("%d",&guess_value);//输入一个数
        nbr_of_guesses ++;//猜测次数+1

        if(number == guess_value)
        {
            done = YES;
        }
        else
        {
            if(number < guess_value)
            {
                printf("/nLower,Please!");
            }
            if(number > guess_value)
            {
                printf("/nHigher,Please!");
            }
        }

    }
    printf("/n/nCongratulations! You guessed right in %d Guesses!"
        ,nbr_of_guesses);
    printf("/n/nThe number was %d/n/n",number);
    return 0;
}