void main(){ int a; printf("input integer number: "); scanf("%d",&a); switch (a){ case 1:printf("Monday/n"); case 2:printf("Tuesday/n"); case 3:printf("Wednesday/n"); case 4:printf("Thursday/n"); case 5:printf("Friday/n"); case 6:printf("Saturday/n"); case 7:printf("Sunday/n"); default:printf("error/n"); } }
void main(){ int a; printf("input integer number: "); scanf("%d",&a); switch (a){ case 1:printf("Monday/n");break; case 2:printf("Tuesday/n"); break; case 3:printf("Wednesday/n");break; case 4:printf("Thursday/n");break; case 5:printf("Friday/n");break; case 6:printf("Saturday/n");break; case 7:printf("Sunday/n");break; default:printf("error/n"); } }
在使用switch语句时还应注重以下几点:
1.在case后的各常量表达式的值不能相同,否则会出现错误。
2.在case后,答应有多个语句,可以不用{}括起来。
3.各case和default子句的先后顺序可以变动,而不会影响程序执行结果。
4.default子句可以省略不用。程序举例
输入三个整数,输出最大数和最小数。
void main(){ int a,b,c,max,min; printf("input three numbers: "); scanf("%d%d%d",&a,&b,&c); if(a>b) {max=a;min=b;} else {max=b;min=a;} if(max<c) max=c; else if(min>c) min=c; printf("max=%d/nmin=%d",max,min); }