曾經幫別人答過,程式碼如下:
#include
int main()
{
int nAppValue, nUserValue, iCount = 0;
srand( time( 0 ) );
nAppValue = rand() % 100 + 1;
printf( "請猜我選中的是哪個數字(1-100)?\n" );
do
scanf( "%d", &nUserValue );
++iCount;
if( nUserValue == nAppValue )
printf( "猜了%d次, 猜中了!\n",iCount );
break;
}
else if( nUserValue > nAppValue )
printf( "錯了!再小點, 繼續猜!\n" );
else
printf( "錯了!再大點!繼續猜!\n" );
}while(1);
printf( "遊戲結束!\n" );
return 0;
曾經幫別人答過,程式碼如下:
#include
#include
#include
int main()
{
int nAppValue, nUserValue, iCount = 0;
srand( time( 0 ) );
nAppValue = rand() % 100 + 1;
printf( "請猜我選中的是哪個數字(1-100)?\n" );
do
{
scanf( "%d", &nUserValue );
++iCount;
if( nUserValue == nAppValue )
{
printf( "猜了%d次, 猜中了!\n",iCount );
break;
}
else if( nUserValue > nAppValue )
{
printf( "錯了!再小點, 繼續猜!\n" );
}
else
{
printf( "錯了!再大點!繼續猜!\n" );
}
}while(1);
printf( "遊戲結束!\n" );
return 0;
}