一个简单的猜数字游戏

# include <stdio.h>
# include <stdlib.h>   //提供srand(),rand()函数原型
# include <time.h>    //提供time()函数原型
int number(int n);    // n 代表数字的最大值,提供给函数求模


int main()
{
    int num;
    int guess;
    int limit;
    srand((unsigned int)time(NULL));
    
    puts("请设定最大的数字(正整数),q结束:");
    while((scanf("%d",&limit))==1)
   {
    num = number(limit);          //给num赋值
    puts("猜数字开始,请输入数字,q结束:");
    while((scanf("%d",&guess))==1)
      {
        if(guess>num)
        {
           puts("");
           continue;
        }
        else if(guess < num)
        {
            puts("");
            continue;
        }
        else
        {
            puts("猜中了!");
            break;
        }        
      }
    puts("请再次设定最大的数字(正整数),q结束:");
   }
    puts("Bye.");
    
}

int number(int n)
{
    int a;
    a = rand()%n;
    return a; 
}

 

posted @ 2022-01-26 14:48  Tolerieren  阅读(36)  评论(0)    收藏  举报