/*************************************************
* 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;
}
|
阅读(1463) | 评论(0) | 转发(0) |