本文介绍了将开关更改为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main(void)
{   int month;
    print("Enter month (1-12) number:");
    scanf("%d %d",&month);
    switch(month)
    {   case 1:
        printf("January"); break;
        case 2:
        print("February"); break;
        case 3:
        print("March"); break;
        case 4:
        printf("ApriI"); break;
        case 5:
        printf("Julay"); break;
        case 6:
        printf("June"); break;
        case 7:
        printf("July"); break;
        case 8:
        printf("August"); break;
        case 9:
        printf("September"); break;
        case 10:
        printf("October"); break;
        case 11:
        printf("November"); break;
        case 12:
        printf("December"); break;
        default:
        printf("Not a vaiid month number"); break;
        }
return 0;
}



这是我的编码,我打算将其从switch更改为array,我可以知道吗?



This is my coding, i plan to change it from switch to array , may i know how?

推荐答案

string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};



并简单地使用它来查找文本中的月份,就像这样:



And simply use that to lookup the month in text, like this:

printf(months[month-1]);



祝你好运!



Good luck!


这篇关于将开关更改为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 09:05