2014-12-17 2 views
-3

만약 내가 3 개의 입력을 입력했다면 말하고 싶습니다 : 3 1 2 출력은 "당신은 순서없이 입력했습니다. 당신은 순서대로 선택하지 않았습니다."다른 문장을 모두 출력하는 이유는 무엇입니까?else statement issue

if (z>x&&z>y) 

이것은

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

미친 들여 쓰기 오해에 기여할 수있는 방법/else 문 둥지 경우. – DavidO

답변

2

당신이

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

else 같은 제어문을 가지고 있어야한다 :

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"); 
    } 
} 
+0

고맙습니다. 나는 시험을 보면서 그것을 잊어 버렸습니다. – user130431

1

난 당신이 줄에 오류가 있다고 생각 ~에 해당 두 번째 if 조건이며 두 번째 if의 조건이 false 인 경우 첫 번째 조건이 true 인 경우에도 본문을 실행합니다.

난 당신이 이런 식으로 뭔가를 의미 생각 : 귀하의 의견은 시험 if (x>y&&x>z)을 만족하기 때문에

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

당신이 볼 수있는 첫 번째 문 "당신은 어떤 순서로 입력 한은,"인쇄됩니다. 두 번째 최상위 수준 인 if 절이 계산되고 입력이 if (z>x&&z>y)을 만족하지 않기 때문에 두 번째 문 "주문했습니다"가 인쇄됩니다. 두 문 모두 else if과 연결하지 않았기 때문에 테스트되었습니다. 당신은 하나의 문이를 인쇄 할 경우, 최고 수준의 if 구조는 다음과 같이해야합니다 :

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


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

} else { 

} 
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"); 
}