#include <stdio.h> int main(){ int a; printf("Input integer number:"); scanf("%d",&a); if(a==1){ printf("Monday\n"); }else if(a==2){ printf("Tuesday\n"); }else if(a==3){ printf("Wednesday\n"); }else if(a==4){ printf("Thursday\n"); }else if(a==5){ printf("Friday\n"); }else if(a==6){ printf("Saturday\n"); }else if(a==7){ printf("Sunday\n"); }else{ printf("error\n"); } return 0; }运行结果:
#include <stdio.h> int 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"); break; } return 0; }运行结果:
switch(表达式){
case 整型数值1: 语句 1;
case 整型数值2: 语句 2;
......
case 整型数值n: 语句 n;
default: 语句 n+1;
}
#include <stdio.h> int 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"); } return 0; }运行结果:
break;
语句。#include <stdio.h> int 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"); break; } return 0; }运行结果:
break;
语句。case 10: printf("..."); break; //正确 case 8+9: printf("..."); break; //正确 case 'A': printf("..."); break; //正确,字符和整数可以相互转换 case 'A'+19: printf("..."); break; //正确,字符和整数可以相互转换 case 9.5: printf("..."); break; //错误,不能为小数 case a: printf("..."); break; //错误,不能包含变量 case a+10: printf("..."); break; //错误,不能包含变量
本文链接:http://task.lmcjl.com/news/8077.html