卡在 c 项目中的 switch

stuck with switch in c project

我正在尝试用 C 编写一个项目 但它不起作用

谁能告诉我为什么?我需要改变什么? 我正在尝试计算明年某个人的生日是一周中的哪一天 在他告诉我今年的日期之后(例如 14/11 第 2 天(星期一)

#include<stdio.h>

int day,month,day_in_week;

void main()
{
    printf("Enter the date of your birthday in 2015 (day,month, day in week):\n");
    scanf("%d%d%d",&day,&month,&day_in_week);

   if(day_in_week<1 || day_in_week>7)
   {
       printf("Error date entered");
       return;
   }
   if(month<1 || month>12)
   {
       printf("Error date entered");
       return;
   }
   switch(month)
   {
   case 2:
       if(day<1 || day>28)
              {
                  printf("Error date entered");
                  return;
              }
   case 1: case 3:case 5:case 7:case 8:case 10:case 12:
       if(day<1 || day>31)
              {
                  printf("Error date entered");
                  return;
              }
   case 4:case 6:case 9:case 11:
       if(day<1 || day>30)
              {
                  printf("Error date entered");
                  return;
              }
   }

   switch(month)
   {
   case 1:case 2:
           day_in_week++;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
   case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:
              day_in_week=day_in_week+2;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
   }

   switch(day_in_week)
   {
   case 1:
       printf("Sunday");
   case 2:
       printf("Monday");
   case 3:
       printf("Tuesday");
   case 4:
       printf("Wednesday");
   case 5:
       printf("Thursday");
   case 6:
       printf("Friday");
   case 7:
       printf("Saturday");
   }
   getch();
   return 0;
}

您应该添加 break:

switch(month)
   {
   case 1:case 2:
           day_in_week++;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
           break;
   case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:
              day_in_week=day_in_week+2;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
           break;
   }

你也应该在接下来 switch 中这样做。
因此,您的代码应如下所示:

#include<stdio.h>

int day,month,day_in_week;

void main()
{
    printf("Enter the date of your birthday in 2015 (day,month, day in week):\n");
    scanf("%d%d%d",&day,&month,&day_in_week);

   if(day_in_week<1 || day_in_week>7)
   {
       printf("Error date entered");
       return;
   }
   if(month<1 || month>12)
   {
       printf("Error date entered");
       return;
   }
   switch(month)
   {
   case 2:
       if(day<1 || day>28)
              {
                  printf("Error date entered");
                  return;
              }
   case 1: case 3:case 5:case 7:case 8:case 10:case 12:
       if(day<1 || day>31)
              {
                  printf("Error date entered");
                  return;
              }
   case 4:case 6:case 9:case 11:
       if(day<1 || day>30)
              {
                  printf("Error date entered");
                  return;
              }
   }

   switch(month)
   {
   case 1:case 2:
           day_in_week++;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
           break;
   case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:
              day_in_week=day_in_week+2;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
           break;
   }

   switch(day_in_week)
   {
   case 1:
       printf("Sunday");
       break;
   case 2:
       printf("Monday");
       break;
   case 3:
       printf("Tuesday");
       break;
   case 4:
       printf("Wednesday");
       break;
   case 5:
       printf("Thursday");
       break;
   case 6:
       printf("Friday");
       break;
   case 7:
       printf("Saturday");
       break;
   }
   getch();
   return 0;
}

这里有个小小的帮助,让你看得更清楚:

Switch case statements are a substitute for long if statements that compare a variable to several "integral" values. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.

switch ( <variable> ) {
case this-value:
  Code to execute if <variable> == this-value
  break;
case that-value:
  Code to execute if <variable> == that-value
  break;
...
default:
  Code to execute if <variable> does not equal the value following any of the cases
  break;
}

一个 c switch 命令将保持 运行 从一个案例到下一个案例,除非在案例结束时有一个 break 命令。

switch (x) {
case 1:
  printf ( "case 1\n" );
//no break command, if x == 1, it keeps going
case 2:
  printf ( "case 1 and 2!\n" );
  break; // break command, it doesn't keep going to 3

case 3:
  printf ( "case 3 only\n" );
}

问题是因为每个 case 的末尾都没有 break。一旦你在每个 case 的末尾添加 break (在最后),就应该修复它。