#C语言#求某年某月的天数,结果错了。

求某年某月天数,不是闰年的2月输出也是29天,代码如下图:
改了几次都是这个结果,是不是有什么小点漏了,还是这种写法不好。
请问错误发生在哪里,应该怎么改?

img

if里面的双引号去掉,如果对你有帮助,麻烦采纳下,谢谢~~~

img

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7581867
  • 你也可以参考下这篇文章:解决结构体不对齐,找到对应字段的一个简单方法
  • 除此之外, 这篇博客: 从键盘输入某年某月(包括闰年),编程输出该年的该月拥有的天数中的 从键盘输入某年某月(包括闰年),编程输出该年的该月拥有的天数。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    **输入格式要求:"%d,%d" 提示信息:“Input year,month:” “The number of days is %d\n”
    **输出格式要求:"%d"
    程序运行示例1如下:
    Input year,month:1984,2
    The number of days is 29
    程序运行示例2如下:
    Input year,month:2000,2
    The number of days is 29
    程序运行示例3如下:
    Input year,month:1985,2
    The number of days is 28
    程序运行示例4如下:
    Input year,month:1983,13
    Input year,month:1983,-1
    Input year,month:1983,1
    The number of days is 31

    #include<stdio.h>
    #define MONTHS 12
    
    int main(void)
    {
        int days[2][MONTHS] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
    	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
        int year, month;
        do
        {
            printf("Input year,month:");
            scanf("%d,%d", &year, &month );
        }
        while (month < 1 || month > 12);
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
            printf("The number of days is %d\n", days[1][month - 1]);
        else
            printf("The number of days is %d\n", days[0][month - 1]);
        return 0;
    }
    
  • 您还可以看一下 千锋老师的什么是云计算? 课程中的 杨哥知识普及小课堂之 什么是云计算?小节, 巩固相关知识点

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^