【发布时间】:2021-07-24 23:25:14
【问题描述】:
在这里,如果我运行代码,每个案例都会被打印出来,如果我把默认值放在最后,它就不会发生,你能告诉我为什么会这样吗? 非常感谢您花时间回答我的问题。
在此处输入代码
#include <stdio.h>
int main()
{
int weekday=8;
switch (weekday)
{
default:
printf("\n Please enter Valid Number between 1 to 7");
case 1:
printf("\n Today is Monday");
case 2:
printf("\n Today is Tuesday");
case 3:
printf("\n Today is Wednesday");
case 4:
printf("\n Today is Thursday");
case 5:
printf("\n Today is Friday");
case 6:
printf("\n Today is Saturday");
case 7:
printf("\n Today is Sunday");
}
printf("\n%d",weekday);
return 0;
}
【问题讨论】:
-
您忘记将
break语句放入您的switch语句中。因此,案例的顺序(包括默认案例)很重要,因为每个案例都会落到其后。
标签: c++ c switch-statement