c语言中switch语句
1、
#include <stdio.h>
int main(void)
{
int i;
puts("please input an integer.");
printf("i = "); scanf("%d", &i);
if (i % 3 == 0)
puts("can be devided");
else
{
if (i % 3 == 1)
puts("remainder is 1.");
else
puts("remainder is 2.");
}
return 0;
}
↓
#include <stdio.h>
int main(void)
{
int i;
puts("please input an integer.");
printf("i = "); scanf("%d", &i);
switch (i % 3)
{
case 0 : puts("can be devided."); break;
case 1 : puts("remainder is 1."); break;
case 2 : puts("remainder is 2."); break;
}
return 0;
}
2、
#include <stdio.h>
int main(void)
{
int i;
puts("please input an integer.");
printf("i = "); scanf("%d", &i);
switch (i)
{
case 1 : puts("a"); puts("b"); break;
case 2 : puts("c");
case 3 : puts("d"); break;
case 4 : ; break;
case 5 : puts("e"); break;
case 6 :
case 7 : puts("f"); break;
default : puts("g"); break;
}
return 0;
}
3、
#include <stdio.h>
int main(void)
{
int i;
puts("please input an month.");
printf("i = "); scanf("%d", &i);
switch (i)
{
case 3 : puts("spring"); break;
case 4 : puts("spring"); break;
case 5 : puts("spring"); break;
case 6 : puts("summer"); break;
case 7 : puts("summer"); break;
case 8 : puts("summer"); break;
case 9 : puts("autumn"); break;
case 10 : puts("autumn"); break;
case 11 : puts("autunm"); break;
case 12 : puts("winter"); break;
case 1 : puts("winter"); break;
case 2 : puts("winter"); break;
default : puts("not exist"); break;
}
return 0;
}