关于#C语言#的问题,如何解决?

C语言输入问题
问题:为什么我输入数字后程序没有继续往下执行,而是重复输入

#include 
int main()
{    
    printf("Please switch the number:\n");
    printf("1,Single \t\t\t\t2,Householeder\n3,Married(co-ownership) \t\t4,Married(divorce)\n");
    int num1;
    double salary;
    
    while (scanf("%d", &num1)==1);
    {    
        
        salary = 0;
        printf("Please enter your salary:");
        scanf("%lf", &salary);
        switch (num1)
        {
        case 1:if (salary >= 17850) printf("You need to pay tax $%lf.", 0.15 * 17850 + 0.28 * (salary - 17850));
              else printf("You need to pay tax $%lf.", 0.15 * salary); break;
        case 2:if (salary >= 23900) printf("You need to pay tax $%lf.", 0.15 * 23900 + 0.28 * (salary - 23900));
              else printf("You need to pay tax $%lf.", 0.15 * salary); break;
        case 3:if (salary >= 29750) printf("You need to pay tax $%lf.", 0.15 * 29750 + 0.28 * (salary - 29750));
              else printf("You need to pay tax $%lf.", 0.15 * salary); break;
        case 4:if (salary >= 14875) printf("You need to pay tax $%lf.", 0.15 * 14875 + 0.28 * (salary - 14875));
              else printf("You need to pay tax $%lf.", 0.15 * salary); break;
        }
        
    }

    return 0;
}



img

第九行你多写了一个分号
造成了一直循环输入

  • while() { } 这是带有循环体的循环语句
  • while(); 这是不带循环体的,只会一直执行该条语句
    #include <stdio.h>
    int main()
    {    
      printf("Please switch the number:\n");
      printf("1,Single \t\t\t\t2,Householeder\n3,Married(co-ownership) \t\t4,Married(divorce)\n");
      int num1;
      double salary;
      
      while (scanf("%d", &num1)==1)
      {    
          
          salary = 0;
          printf("Please enter your salary:");
          scanf("%lf", &salary);
          switch (num1)
          {
          case 1:if (salary >= 17850) printf("You need to pay tax $%lf.", 0.15 * 17850 + 0.28 * (salary - 17850));
                else printf("You need to pay tax $%lf.", 0.15 * salary); break;
          case 2:if (salary >= 23900) printf("You need to pay tax $%lf.", 0.15 * 23900 + 0.28 * (salary - 23900));
                else printf("You need to pay tax $%lf.", 0.15 * salary); break;
          case 3:if (salary >= 29750) printf("You need to pay tax $%lf.", 0.15 * 29750 + 0.28 * (salary - 29750));
                else printf("You need to pay tax $%lf.", 0.15 * salary); break;
          case 4:if (salary >= 14875) printf("You need to pay tax $%lf.", 0.15 * 14875 + 0.28 * (salary - 14875));
                else printf("You need to pay tax $%lf.", 0.15 * salary); break;
          }
          
      }
    
      return 0;
    }
    

望采纳!!!
因为你的程序中的 while (scanf("%d", &num1)==1); 后面的花括号是多余的,而且在 while 循环中没有任何逻辑来更新循环条件,因此这个循环会一直重复。

试着把 while (scanf("%d", &num1)==1); 删掉,或者把它改成 while (scanf("%d", &num1)==1) {...} 。这样程序就只会在第一次输入时执行一次了。

你可以在switch语句后面加上一个break语句来结束循环。

望采纳!!!点击回答右侧采纳即可!!
这是因为在while循环中,没有添加循环体,因此程序会一直重复输入,而不会继续往下执行。要让程序继续往下执行,需要在while循环中添加循环体,如下所示:

#include <stdio.h>
int main()
{    
    printf("Please switch the number:\n");
    printf("1,Single \t\t\t\t2,Householeder\n3,Married(co-ownership) \t\t4,Married(divorce)\n");
    int num1;
    double salary;
    
    while (scanf("%!d(MISSING)", &num1)==1)
    {    
        salary = 0;
        printf("Please enter your salary:");
        scanf("%!l(MISSING)f", &salary);
        switch (num1)
        {
        case 1:if (salary >= 17850) printf("You need to pay tax $%!l(MISSING)f.", 0.15 * 17850 + 0.28 * (salary - 17850));
              else printf("You need to pay tax $%!l(MISSING)f.", 0.15 * salary); break;
        case 2:if (salary >= 23900) printf("You need to pay tax $%!l(MISSING)f.", 0.15 * 23900 + 0.28 * (salary - 23900));
              else printf("You need to pay tax $%!l(MISSING)f.", 0.15 * salary); break;
        case 3:if (salary >= 29750) printf("You need to pay tax $%!l(MISSING)f.", 0.15 * 29750 + 0.28 * (salary - 29750));
              else printf("You need to pay tax $%!l(MISSING)f.", 0.15 * salary); break;
        case 4:if (salary >= 14875) printf("You need to pay tax $%!l(MISSING)f.", 0.15 * 14875 + 0.28 * (salary - 14875));
              else printf("You need to pay tax $%!l(MISSING)f.", 0.15 * salary); break;
        }
        
    }
    return 0;
}