如果是else语句问题

时间:2014-12-17 03:31:30

标签: c

我想知道如果我输入了3个输入:3 1 2输出给了我"你没有按顺序输入你没有选择"为什么打印其他语句?

int main ()
{

    printf("Please enter 3 integer and will checked if they are in acd or dec: ");
    int x,y,z;
    char trash;
    scanf("%i",&x);
    scanf("%c",&trash);
    scanf("%i",&y);
    scanf("%c",&trash);
    scanf("%i",&z);

    if (x>y&&x>z)
    {
        if (y>z)
        {
            printf("you've entered in decending order ");
        }else{
            printf("you've entered in no order");
        }

    }if (z>x&&z>y)
    {

        if(y>x)
            {
        printf("you've printed in ascending order");
            }
        else
            {printf("you've printed in no order");

        }

    }else{
        printf("You've chosen in no order");
    }
}

4 个答案:

答案 0 :(得分:2)

你有像这样的控制语句

if (x>y&&x>z) ...
if (z>x&&z>y) ...
else

else对应于第二个if条件,并且如果第二个if中的条件为假,即使第一个条件为真,也将执行其正文。

我认为你的意思是做这样的事情:

if (x>y&&x>z) ...
else if (z>x&&z>y) ...
else

答案 1 :(得分:2)

您看到的第一个声明,"您已无法订购,"打印是因为您的输入符合测试if (x>y&&x>z)。然后评估第二个顶级if子句,并且第二个语句"您已按顺序选择"打印是因为您的输入不满足if (z>x&&z>y)。两个语句都经过测试,因为您没有将它们与else if连接起来。如果您只想打印一个语句,则顶级if结构需要如下所示:

if (x > y && x > z)
{


} else if (z > x && z > y)
{

} else {

}

答案 2 :(得分:1)

我认为你在这一行中有错误:

if (z>x&&z>y)

这应该是

else if (z>x&&z>y)

答案 3 :(得分:0)

简短版:

if (x>y&&y>z){
   printf("you've entered in decending order ");
}else if (z>y&&y>x) {
   printf("you've printed in ascending order");
}else {
   printf("you've printed in no order");
}
相关问题