C语言 7.0
作者:互联网
今天来讲switch语句的应用;
如有偏颇,还望海涵;
int main()
{
int iDay;
printf("enter a day of week to get cours\n");
scanf("%d",iDay);
switch(iDay)
{
case 1:
printf("have a meeting in my company\n");
break;
case 2:
case 3:
case 4:
case 5:
printf("working with partner\n");
case 6:
printf("go shopping with friends\n");
case 7:
printf("at home with families\n");
default:
printf("you enter is wrong\n");
}
return 0;
}
//switch和if语句各有千秋,在语句少的情况下,进行if语句,switch语句在多条条件下用,当然也可以进行穿插使用;举个例子;
int main()
{
int iDay,iMonth;
printf("please the month you want to kow day\n");
scanf("%d",&iMonth);
switch(iMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
iDay=31;
break;
case 4:
case 6:
case 9:
case 11:
iDay=30;
break;
case 2:
iDay=28;
break;
default:
iDay=-1;
break;
}
if(iDay==-1)
{
printf("the enter is wrong\n");
}
else
{
printf("2021.%d month have %d day\n",iMonth,iDay);
}
return 0;
}
使用switch语句和if语句,可以完成简化;当然练习更关键;
在switch语句中众多case是平级的,一起进行;
标签:case,语句,iDay,switch,C语言,break,7.0,printf 来源: https://blog.51cto.com/15098536/2622796