c语言编个小程序 输入年和月 输出该年有多少天 该月有多少天

#include<stdio.h>
main(int argc, char *argv[]){
int year, month, day; //define variable
printf("Enter year and month:\n");
scanf("%d,%d", &year, &month);
if((year%4==0)&&(year%100!=0)||(year%400==0))
{
if(month==1)
{ day=31;
printf("The date of this year %d is: 366\n",year);
printf("Janurary has %d days\n",day);}
else if (month==2)
{ day=29;
printf("The date of this year %d is: 366\n",year);
printf("February has %d days\n",day);}
else if (month==3)
{ day=31;
printf("The date of this year %d is: 366\n",year);
printf("March has %d days\n",day); }
else if (month==4)
{ day=30;
printf("The date of this year %d is: 366\n",year);
printf("April has %d days\n",day); }
else if (month==5)
{ day=31;
printf("The date of this year %d is: 366\n",year);
printf("May has %d days\n",day); }
else if (month==6)
{ day=30;
printf("The date of this year %d is: 366\n",year);
printf("Jun has %d days\n",day); }
else if (month==7)
{ day=31;
printf("The date of this year %d is: 366\n",year);
printf("July has %d days\n",day); }
else if (month==8)
{ day=31;
printf("The date of this year %d is: 366\n",year);
printf("August has %d days\n",day); }
else if (month==9)
{ day=30;
printf("The date of this year %d is: 366\n",year);
printf("September has %d days\n",day); }
else if (month==10)
{ day=31;
printf("The date of this year %d is: 366\n",year);
printf("October has %d days\n",day); }
else if (month==11)
{ day=30;
printf("The date of this year %d is: 366\n",year);
printf("November has %d days\n",day); }
else (month==12);
{ day=31;
printf("The date of this year %d is: 366\n",year);
printf("December has %d days\n",day); }

}
剩下的提交不上去 有字数限制。。。
不过其实思路是一样的
我年份所给出的天数是正确的

问题是无论我输入几月 显示的都是
December has 31 days
不知道问题出在哪里了, 请各位高手 大侠们 指点啊~~!~
看到了一定要说两句 拜托了~~~

我把else 改成 else if
还是不对 问题还和以前一样
终于发现问题了 scanf("%d,%d", &year, &month);
应该把%d,%d 中间的逗号去掉 ^_^
现在又出现新的问题 当我输入年和月 举个例子 输入1988 2
出现 The date of this year 1988 is: 366
February has 29 days
The date of this year 1988 is: 366
December has 31 days
程序似乎是运行了2次不知道怎么回事。。。

第1个回答  推荐于2016-01-31
因为这里else (month==12); 没有写if
改成else if(month==12); 就正常了

和逗号是没有关系的"%d,%d",有逗号的时候,
输入数据时也得加逗号,如1985,10
感觉运行两遍是因为else (month==12);
任何条件到这都满足,所以会执行它下面的语句。。
谢谢!
----------------
#include<stdio.h>
void main(void)
{
int year, month, day; //define variable
printf("Enter year and month:\n");
scanf("%d,%d", &year, &month);

if((year%4==0)&&(year%100!=0)||(year%400==0))
{
printf("The date of this year %d is: 366\n",year);

if(month==1)
{
day=31;
printf("Janurary has %d days\n",day);}
else if (month==2)
{
day=29;
printf("February has %d days\n",day);}
else if (month==3)
{
day=31;
printf("March has %d days\n",day); }
else if (month==4)
{
day=30;
printf("April has %d days\n",day); }
else if (month==5)
{
day=31;
printf("May has %d days\n",day); }
else if (month==6)
{
day=30;
printf("Jun has %d days\n",day); }
else if (month==7)
{
day=31;
printf("July has %d days\n",day); }
else if (month==8)
{
day=31;
printf("August has %d days\n",day); }
else if (month==9)
{
day=30;
printf("September has %d days\n",day); }
else if (month==10)
{
day=31;
printf("October has %d days\n",day); }
else if (month==11)
{
day=30;
printf("November has %d days\n",day); }
else if(month==12);
{
day=31;
printf("December has %d days\n",day); }
}
else
{
printf("The date of this year %d is: 365\n",year);

if(month==1)
{
day=31;
printf("Janurary has %d days\n",day);}
else if (month==2)
{
day=28;
printf("February has %d days\n",day);}
else if (month==3)
{
day=31;
printf("March has %d days\n",day); }
else if (month==4)
{
day=30;
printf("April has %d days\n",day); }
else if (month==5)
{
day=31;
printf("May has %d days\n",day); }
else if (month==6)
{
day=30;
printf("Jun has %d days\n",day); }
else if (month==7)
{
day=31;
printf("July has %d days\n",day); }
else if (month==8)
{
day=31;
printf("August has %d days\n",day); }
else if (month==9)
{
day=30;
printf("September has %d days\n",day); }
else if (month==10)
{
day=31;
printf("October has %d days\n",day); }
else if (month==11)
{
day=30;
printf("November has %d days\n",day); }
else if(month==12);
{
day=31;
printf("December has %d days\n",day); }
}
}本回答被提问者采纳
第2个回答  2008-04-01
你还没写完吧!还有printf放到IF外面就可以了,干嘛复制粘贴那么多啊………………
相似回答