|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
int sqrt_02(int question);
void main( void )
{
int question = 49, answer;
answer = sqrt_02( question );
if(question < 0 )
printf( "Error: sqrt returns %d\n, answer" );
else
printf( "The square root of %d is %d\n", question, answer );
}
int sqrt_02(int question)
{
int temp=question/2;
while(1)
{
if( temp*temp==question)
return temp;
else
temp-=1;
}
}这个题是为了开平方, 上面此程序输入50进入死循环 因为while(1)的存在 小甲鱼哥你把程序改为
#include <stdio.h>
int sqrt_02(int question);
void main( void )
{
int question = 49, answer;
answer = sqrt_02( question );
if(answer < 0 )
printf( "Error: sqrt returns %d\n, answer" );
else
printf( "The square root of %d is %d\n", question, answer );
}
int sqrt_02(int question)
{
int temp=question/2;
while(temp--)
{
if( temp*temp==question)
return temp;
else
return -1;
}
}
可是这个程序错误,能纠正一下么!此时也是死循环!求大神指点下! 怎么让此程序更加接近人性化!使得程序更加有抵抗力!比如输入错误 能偶提示从新输入!
该贴已经同步到 空手套小白狼的微博 |
|