卡在c项目中的开关

时间:2015-04-27 14:34:48

标签: c switch-statement

我正在尝试用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;
}

3 个答案:

答案 0 :(得分:5)

您应该添加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语句可替代long if语句   比较一个变量与几个&#34;积分&#34;值。   将赋予switch的变量的值与该值进行比较   在每个案例之后,以及当一个值与值相匹配时   变量,计算机继续执行该程序   点。

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

答案 1 :(得分:2)

除非在案例结束时有switch命令,否则c 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" );
}

答案 2 :(得分:2)

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