switch
#include <stdio.h> int main(int argc, char * argv[])
{
int i;
scanf("%d",&i);
switch(i)
{
case :
{printf("春");
break;
}
case :
{
printf("夏");
break;
}
case :
{
printf("秋");
break;
} case :
{ printf("冬");
}
default:
break; }
}
多个case可以运行一个语句块
case中定义变量需要加{};
枚举类型(enum)
1
提高程序的可读性,本质是整形,用枚举类型代表整数常量;
enum Season {
spring,
summer,
autumn,
winter
}
//默认从零开始
enum Season aeason=0;
scanf("%d,&season");
switch(season)
{
case spring:
{
printf("spring");
}
case summer:
{
printf("spring");
}
case autumn:
{
printf("autumn");
]
}
#include <stdio.h>
enum Season {
spring=,
summer,
autumn,
winter
};
typedef enum Season myseason ;//为现有类型创建一个别名
int main(int argc, char * argv[])
{
//默认从零开始
myseason season=;
scanf("%d",&season);
switch(season)
{
case spring:
{ printf("spring");
break; }
case summer: {
printf("summer");
break;
}
case autumn:
{
printf("autumn");
break; }
case winter:
{
printf("winter");
break;
}
default:
{
printf("未知");
} }
}