You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言如何比较输入的月份字符数据判断所属季节?

问题分析
  • 核心错误1:你直接将char数组名inputMonth和整数3比较,数组名在表达式中会退化为指向数组首元素的指针,本质是内存地址和整数对比,条件永远不成立,所以不会有输出
  • 核心错误2:缺少英文月份到数字月份的转换逻辑,你输入的是April这类字符串,不能直接和数字做大小比较
  • 缺少必要头文件:你用到了scanf和printf,需要引入<stdio.h>头文件
修复后的完整代码
#include <stdio.h>
#include <string.h>

int main(void) {
    char inputMonth[50];
    int inputDay;
    int month = 0;
    
    scanf("%s", inputMonth);
    scanf("%d", &inputDay);
    
    // 转换英文月份为数字月份
    if (strcmp(inputMonth, "January") == 0) month = 1;
    else if (strcmp(inputMonth, "February") == 0) month = 2;
    else if (strcmp(inputMonth, "March") == 0) month = 3;
    else if (strcmp(inputMonth, "April") == 0) month = 4;
    else if (strcmp(inputMonth, "May") == 0) month = 5;
    else if (strcmp(inputMonth, "June") == 0) month = 6;
    else if (strcmp(inputMonth, "July") == 0) month = 7;
    else if (strcmp(inputMonth, "August") == 0) month = 8;
    else if (strcmp(inputMonth, "September") == 0) month = 9;
    else if (strcmp(inputMonth, "October") == 0) month = 10;
    else if (strcmp(inputMonth, "November") == 0) month = 11;
    else if (strcmp(inputMonth, "December") == 0) month = 12;
    else {
        printf("Invalid month input");
        return 0;
    }
    
    // 季节判断逻辑
    if ((month == 3 && inputDay >=20) || (month >3 && month <6) || (month ==6 && inputDay <=20)) {
        printf("Spring");
    } else if ((month ==6 && inputDay >=21) || (month >6 && month <9) || (month ==9 && inputDay <=21)) {
        printf("Summer");
    } else if ((month ==9 && inputDay >=22) || (month >9 && month <12) || (month ==12 && inputDay <=20)) {
        printf("Autumn");
    } else {
        printf("Winter");
    }
    
    return 0;
}

内容的提问来源于stack exchange,提问作者CMOS_BATTERY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 03:18:04