我使用这个程序将输入 mm 作为一年中的月份并打印出月份的名称:
#include <stdio.h>
#include <string.h>
int main(){
int mm;
printf("input month ");
scanf("%d", &mm);
char mname[9];
if (mm == 1) {mname = "January";}
if (mm == 2) {mname = "February";}
if (mm == 3) {mname = "March";}
if (mm == 4) {mname = "April";}
if (mm == 5) {mname = "May";}
if (mm == 6) {mname = "June";}
if (mm == 7) {mname = "July";}
if (mm == 8) {mname = "August";}
if (mm == 9) {mname = "September";}
if (mm == 10) {mname = "October";}
if (mm == 11) {mname = "November";}
if (mm == 12) {mname = "December";}
printf("%d is month %s", mm, mname);
return 0;
}
它对数组类型的表达式进行了错误赋值。请帮忙
Taking 迈克尔·沃尔兹两条很棒的评论并将其添加为答案:
#include <stdio.h>
#include <string.h>
void main(int argc, char** argv)
{
int mm = 0;
printf("Please enter a month number [1-12]:\n");
scanf("%d", &mm);
static const char* months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
if (mm >= 1 && mm <= 12)
{
printf("%d is month %s", mm, months[mm - 1]);
}
else
{
printf("You have entered an invalid month number %d\n", mm);
}
}
有效性检查已完成(在上面的评论中提到)。
希望能帮助到你。
Cheers,
Guy.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)