2014-09-26 3 views
-1

이 스크립트를 실행하면 "최대 허풍 속도 # 1 입력 : "이라는 사례 1의 줄에 "34"를 입력하면 반환됩니다. "매듭 속의 바람 속도 : 25.6789" "허리케인 # 2의 최대 풍속을 입력하십시오 :"하지만 "Hurrican의 풍속이 올바르지 않습니다. 다시 시도하십시오."If/else 문이 while 루프 내에서 무시됩니다.

#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char *argv[]) 
{ 
int x; 
int year; 
int tropical_storms; 
int hurricanes; 
int a=1; 
int windspeed; 
float knots; 
float conversion=0.868976; 
    printf("     HEg v1.0 \n"); 
    printf("0. Exit\n"); 
    printf("1. Submit Hurricane Season Storm Information\n"); 
    printf("2. Submit Storm Data\n"); 
    printf("3. Print Hurricane Season Analysis\n"); 
    printf("4. Print Storm Analysis\n"); 
    printf("\n"); 
    printf("Please Enter Selection: "); 
    scanf("%d", &x); 
    switch(x) { 
    case 0: 
      printf("Thank you for using HEg v1.0"); 
      break; 
    case 1: 
      printf("Enter the year: "); 
      scanf("%d", &year); 
      printf("Enter the number of tropical storms in 2009: "); 
      scanf("%d", &tropical_storms); 
      printf("Enter the number of Hurricanes in 2009: "); 
      scanf("%d", &hurricanes); 
      while(a<=hurricanes){ 
       printf("Enter the max windspeed of hurricane #%d: ", a); 
       scanf("%d", &windspeed); 
       a++; 
       if(windspeed>74){ 
       knots=windspeed*conversion; 
       printf("Wind speed in knots: %.4f \n", knots); 
       } 
       else{ 
       printf("Invalid Windspeed for Hurricane. Try again"); 
       } 
      } 

      break; 
    case 2: 
      printf("Menu option not available in HEg v1.0"); 
      break; 
    case 3: 
      printf("Menu option not available in HEg v1.0"); 
      break; 
    case 4: 
      printf("Menu option not available in HEg v1.0"); 
      break; 
    default: 
      printf("no");  
      break; 
    } 
    system("PAUSE>nul"); 
    return 0; 
} 
+3

왜'if' /'else' 문이 무시된다고 생각합니까? –

+1

숙련 된 프로그래머로서 'if'진술이 무시되지 않는다는 것을 확신 할 수 있습니다. 실제로 일어난 일 * 및 *에 대해 * 귀하의 게시물을 업데이트하십시오. 또한이 동작을 유발하기 위해 입력 한 내용을 설명하십시오. –

+1

"진술 내용의 내용"이 의미하는 바가 명확하지 않습니다. –

답변

0

if-else에 printf 문 중 하나가 표시됩니까?
사실, while 루프에 도달하지 못했을 수도 있습니다.
값이 a 인 경우 1이고 허리케인 값으로 무엇을 테스트하고 있습니까?

어드 바이스 단어 : a, x, y 등의 변수가 아니라 더 구체적으로 변수 이름을 지정해야합니다. 그것은 디버깅과 주석 처리에 정말로 도움이 될 것입니다.

2

이상적으로는 작동해야하지만 여전히 scanf 읽기에 의문을 제기 할 수 있습니다. 변수를 읽고 Enter 키를 누를 때마다 scanf는 버퍼에 계속 입력하고 다음 while 문에 입력으로 제공합니다. 명령문에서 실패하면 else 문이 실행됩니다. 정수로 입력하는 것은 악마입니다. atoi 함수를 사용하여 문자열로 입력하고 정수로 변환하십시오.

관련 문제