求教关于C语言的问题(01)
求 x>0,y=1/x;
x<0,y=-1/(2*x);
x=0,y=0;
main(){
double x,y1,y2;
printf("input x:\n");
scanf("%lf",&x);
if(x>0)
y1=1/x;
printf("the result is %lf",y1);
else if(x<0)
y2=-1/(2*x);
printf("the result is %lf",y2);
else
y=0;
printf("the result is zero");
}
为什么我compile:syntax error before "else" 明显printf后面有;号的啊
[解决办法]
把该加的括号都加上试下
[解决办法]
main(){
double x,y1,y2;
printf("input x:\n");
scanf("%lf",&x);
if(x>0)
{
y1=1/x;
printf("the result is %lf",y1);
}
else if(x<0)
{ y2=-1/(2*x);
printf("the result is %lf",y2);
}
else
{ y=0;
printf("the result is zero");
}
}
[解决办法]
- C/C++ code
double x,y,y1,y2; printf("input x:\n"); scanf("%lf",&x); if(x>0) { y1=1/x; printf("the result is %lf",y1); } else if(x<0) { y2=-1/(2*x); printf("the result is %lf",y2); } else { y=0; printf("the result is zero"); }
[解决办法]
把要判断的语句都用{}括起来