Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532753
  • 博文数量: 150
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-17 00:19
文章分类

全部博文(150)

文章存档

2011年(1)

2009年(14)

2008年(135)

我的朋友

分类: LINUX

2008-08-12 17:32:45

/*************************************************
 * int rand(void);
 * Gusse the number : *
 * *
 * *
 * *
 * ************************************************/

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

int main()
{
     int iGusse , iSecret;
     iSecret = rand() % 10+1 ;
     printf("please enter the integer( from 1 to 10):");
     scanf( "%d", &iGusse);
    do{
        if( iGusse < iSecret )
        {
          printf( "the input integer is smaller!\n re-input:" );
          scanf( "%d", &iGusse );
        }
        else if( iGusse > iSecret )
        {
          printf( " the input integer is bigger! \n re-input: " );
          scanf( "%d" , &iGusse );
        }
        else
          { printf( "congratulations! " );}
    }while( iGusse != iSecret );

      return 0;
}


电脑上随机值是4,但是经过3,5,4输入后,并不显示congratulations
,原因是scanf( "%d" , &iGusse );语句,再重新输入后,并不执行
else

          { printf( "congratulations! " );}
而是重新一次循环。这时如果新的iGusse等于了iSecret,则不会再一次循环,故不会输出congratulations.
而应该这样的代码:

/*************************************************
 * int rand(void);
 * Gusse the number : *
 * *
 * *
 * *
 * ************************************************/



//#include

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

int main()
{
     int iGusse , iSecret;
     iSecret = rand() % 10+1 ;
     printf("please enter the integer( from 1 to 10):");
     scanf( "%d", &iGusse);
    do{
        if( iGusse < iSecret )
        {
          printf( "the input integer is smaller!\n re-input:" );
          scanf( "%d", &iGusse );
        }
        else if( iGusse > iSecret )
        {
          printf( " the input integer is bigger! \n re-input: " );
          scanf( "%d" , &iGusse );
        }
    }while( iGusse != iSecret );

            printf( "congratulations!\n " );
      return 0;
}

阅读(1434) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~