C语⾔枚举常量判断⽤户输⼊⽉份,枚举(enum)显⽰⽉份天
数例⼦C语⾔
枚举
定义⼀组命名常量的机制(枚举常量)
表达式:enum类型名称{ 标识符序列 } ;
/*枚举
*/
enum month
{
January = 1,
enum c++
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
/
*main
*/
int main(int argc, const char * argv[]) {
@autoreleasepool {
//显⽰每⽉的天数
int days;
enum month enterMonth;
NSLog(@"请键⼊⽉数:");
do
{
scanf("%d", &enterMonth);
if (enterMonth > 0 && enterMonth < 13)
{
switch (enterMonth)
{
case January:
case March:
case May:
case July:
case August:
case October:
case December:
days = 31;
break;
case April:
case June:
case September:
case November:
days = 30;
break;
case February:
days = 28;
break;
default:
break;
}
NSLog(@"%d⽉有%d天", enterMonth, days); if (enterMonth == 2)
{
NSLog(@"...或者是29天,如果是闰年");
}
}
else
{
NSLog(@"没有这个⽉数:%d", enterMonth);
exit(1);
}
}while (1);
}
return 0;
}
测试结果
2016-05-31 17:28:03.368结构(struct)[4527:173637]请键⼊⽉数:
12
2016-05-31 17:28:05.320结构(struct)[4527:173637] 12⽉有31天
2
2016-05-31 17:28:14.032结构(struct)[4527:173637] 2⽉有28天
2016-05-31 17:28:14.032结构(struct)[4527:173637] ...或者是29天,如果是闰年

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。