如果/ while语句在while循环中被忽略

时间:2014-09-26 01:49:16

标签: c

当我运行这个时,我在第1个案例中说到"输入飓风的最大速度#1:"我将进入" 34"并且它将返回"风速结:25.6789" "输入飓风#2的最大风速:"但我试图让它返回"无效的风速为飓风。再试一次。"

#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;
}

2 个答案:

答案 0 :(得分:2)

理想情况下它应该可以工作但我仍然可以怀疑扫描读数。每当您读取变量并按Enter键时,scanf将保持输入缓冲区并将其作为输入提供给下一个while语句。如果语句和运行else语句导致失败。将输入作为整数是一个魔鬼。将输入作为字符串并使用atoi函数转换为整数。

答案 1 :(得分:0)

您是否在if-else中看到printf个陈述中的任何一个? 事实上,你可能甚至没有进入while循环 值a1,然后您使用什么作为飓风的值进行测试?

建议:您需要更具描述性地命名变量,而不仅仅是axy等;它将真正有助于调试和评论。