- /*
- 用if判断
- */
- int panduan(){
- int a;
- printf("请用户输入月份:\n");
- scanf("%d",&a);
- if (a>12 || a<1) {
- printf("地球没有这个月份\n");
- return 0;
- }else if(a<=3){
- printf("春季\n");
- }else if (a<=6){
- printf("夏季\n");
- }else if (a<=9){
- printf("秋季\n");
- }else{
- printf("冬季\n");
- }
- return 0;
- }
- /*
- switch
- */
- int panduan2(){
- int a;
- printf("请用户输入月份:\n");
- scanf("%d",&a);
- if (a>12 || a<1) {
- printf("地球没有这个月份\n");
- return 0;
- }
- switch (a) {
- case 1:
- case 2:
- case 3:
- printf("春季\n");break;
- case 5:
- case 6:
- case 4:
- printf("夏季\n"); break;
- case 7:
- case 8:
- case 9:
- printf("秋季\n");break;
- default:
- printf("冬季\n");
- break;
- }
- return 0;
- }
- /*switch_2*/
- void switch_2(){
- int month;
- printf("请用户输入月份:\n");
- scanf("%d",&month);
- if (month>12 || month<1) {
- printf("地球没有这个月份\n");
- }else {
- switch (month/3) {
- case 1:
- printf("春季");
- break;
- case 2:
- printf("夏季");
- break;
- case 3:
- printf("秋季");
- break;
- default:
- printf("冬季");
- break;
- }
-
- }
- }
- /**
- * 二维指针数组版
- */
- void func(){
- char *season[5] = {
- "春季",
- "夏季",
- "秋季",
- "冬季",
- "地球没有这个月份!"
- };
- int month;
- printf("请用户输入月份:\n");
- scanf("%d",&month);
- switch (month/3) {
- case 1:
- printf("%s",season[0]);
- break;
- case 2:
- printf("%s",season[1]);
- break;
- case 3:
- printf("%s",season[2]);
- break;
- default:
- printf("%s",season[3]);
- break;
- }
- }
复制代码 |
|