C入门错误?

来源:百度知道 编辑:UC知道 时间:2024/05/12 06:46:05
刚在看C的入门其中有一段代码.
--------------
4.输入三个数x,y,z,然后按从大到小输出。
main()
{
float x,y,z;
scanf("%f%f%f",&x,&y,&z);
if(x>=y&&x>=z)
{
printf("%f\t",x);
if(y>=z) printf("%f\t%f\n",y,z);
else printf("%f\t%f\n",z,y);
}
else if(y>=x&&y>=z)
{
printf("%f\t",y);
if(x>=z) printf("%f\t%f\n",x,z);
else printf("%f\t%f\n",z,x);
}
else
{
printf("%f\t",z);
if(x>=y) printf("%f\t%f\n",x,y);
else printf("%f\t%f\n",y,x);

倒数7行的else没对应关系,应该改为
#include<stdio.h>
main()
{
float x,y,z;
scanf("%f%f%f",&x,&y,&z);
if(x>=y&&x>=z)
{ printf("%f\t",x);
if(y>=z) printf("%f\t%f\n",y,z);
else printf("%f\t%f\n",z,y);
}
else
{if(y>=x&&y>=z)
{printf("%f\t",y);
if(x>=z) printf("%f\t%f\n",x,z);
else printf("%f\t%f\n",z,x);
}
else
{ printf("%f\t",z);
if(x>=y) printf("%f\t%f\n",x,y);
else printf("%f\t%f\n",y,x);
}
}
}

"else" means z>=x&&z>=y in your eyes,isn't it? but it is not that easy as you thought;the compile system think the "else" as all the situation but x>=y&&x>=zandy>=x&&y>=z,including x>=y&&x<=z and so on,that