求救:C语言判断是一年中的第几天
刚开始学C语言.只会这种简单的编程.但是看了好多遍都没发现错误.而系统说我有一个error,请问是哪里出错了.上几次编程也老是错一个.我觉得是我某个地方没注意,所以老是错,~帮忙下~
#include <stdio.h>
int main()
{
int year,month,day;
printf("Enter the year,month,day:\n");
scanf("%d%d%d", &year,&month,&day);
int sum=0;
if(year%400==0||(year%4==0&&year%100!=0))
{
if month==1
sum=0;
else if month==2
sum=31;
else if month==3
sum=31+29;
else if month==4
sum=31+29+31;
else if month==5
sum=31+29+31+30;
else if month==6
sum=31+29+31+30+31;
else if month==7
sum=31+29+31+30+31+30;
else if month==8
sum=31+29+31+30+31+30+31;
else if month==9
sum=31+29+31+30+31+30+31+31;
else if month==10
sum=31+29+31+30+31+30+31+31+30;
else if month==11
sum=31+29+31+30+31+30+31+31+30+31;
else if month==12
sum=31+29+31+30+31+30+31+31+30+31+30;
}
else
{
if month==1
sum=0;
else if month==2
sum=31;
else if month==3
sum=31+28;
else if month==4
sum=31+28+31;
else if month==5
sum=31+28+31+30;
else if month==6
sum=31+28+31+30+31;
else if month==7
sum=31+28+31+30+31+30;
else if month==8
sum=31+28+31+30+31+30+31;
else if month==9
sum=31+28+31+30+31+30+31+31;
else if month==10
sum=31+28+31+30+31+30+31+31+30;
else if month==11
sum=31+28+31+30+31+30+31+31+30+31;
else if month==12
sum=31+28+31+30+31+30+31+31+30+31+30;
}
sum=sum+day;
printf("The sum is %d",sum);
return 0;
}
答案:条件表达式要加括号
#include <stdio.h>
int main()
{
int year,month,day;
printf("Enter the year,month,day:\n");
scanf("%d%d%d", &year,&month,&day);
int sum=0;
if(year%400==0||(year%4==0&&year%100!=0))
{
if (month==1)
sum=0;
else if (month==2)
sum=31;
else if (month==3)
sum=31+29;
else if (month==4)
sum=31+29+31;
else if (month==5)
sum=31+29+31+30;
else if (month==6)
sum=31+29+31+30+31;
else if (month==7)
sum=31+29+31+30+31+30;
else if (month==8)
sum=31+29+31+30+31+30+31;
else if (month==9)
sum=31+29+31+30+31+30+31+31;
else if (month==10)
sum=31+29+31+30+31+30+31+31+30;
else if (month==11)
sum=31+29+31+30+31+30+31+31+30+31;
else if (month==12)
sum=31+29+31+30+31+30+31+31+30+31+30;
}
else
{
if (month==1)
sum=0;
else if (month==2)
sum=31;
else if (month==3)
sum=31+28;
else if (month==4)
sum=31+28+31;
else if (month==5)
sum=31+28+31+30;
else if (month==6)
sum=31+28+31+30+31;
else if (month==7)
sum=31+28+31+30+31+30;
else if (month==8)
sum=31+28+31+30+31+30+31;
else if (month==9)
sum=31+28+31+30+31+30+31+31;
else if (month==10)
sum=31+28+31+30+31+30+31+31+30;
else if (month==11)
sum=31+28+31+30+31+30+31+31+30+31;
else if (month==12)
sum=31+28+31+30+31+30+31+31+30+31+30;
}
sum=sum+day;
printf("The sum is %d",sum);
return 0;
}
一个小错误,是if语句的条件判断,都少了括号,形式应该是 if ( ) else { }
更改如下:
/*刚开始学C语言.只会这种简单的编程.但是看了好多遍都没发现错误.而系统说我有一个error,
请问是哪里出错了.上几次编程也老是错一个.我觉得是我某个地方没注意,所以老是错,*/
#include <stdio.h>
int main()
{
int year,month,day;
printf("Enter the year,month,day:\n");
scanf("%d%d%d", &year,&month,&day);
int sum=0;
if(year%400==0||(year%4==0 && year%100!=0))
{
if(month==1)
sum=0;
else if(month==2)
sum=31;
else if(month==3)
sum=31+29;
else if (month==4)
sum=31+29+31;
else if(month==5)
sum=31+29+31+30;
else if(month==6)
sum=31+29+31+30+31;
else if(month==7)
sum=31+29+31+30+31+30;
else if(month==8)
sum=31+29+31+30+31+30+31;
else if(month==9)
sum=31+29+31+30+31+30+31+31;
else if(month==10)
sum=31+29+31+30+31+30+31+31+30;
else if(month==11)
sum=31+29+31+30+31+30+31+31+30+31;
else if(month==12)
sum=31+29+31+30+31+30+31+31+30+31+30;
}
else
{
if(month==1)
sum=0;
else if(month==2)
sum=31;
else if(month==3)
sum=31+28;
else if(month==4)
sum=31+28+31;
else if(month==5)
sum=31+28+31+30;
else if(month==6)
sum=31+28+31+30+31;
else if(month==7)
sum=31+28+31+30+31+30;
else if(month==8)
sum=31+28+31+30+31+30+31;
else if(month==9)
sum=31+28+31+30+31+30+31+31;
else if(month==10)
sum=31+28+31+30+31+30+31+31+30;
else if(month==11)
sum=31+28+31+30+31+30+31+31+30+31;
else if(month==12)
sum=31+28+31+30+31+30+31+31+30+31+30;
}
sum=sum+day;
printf("The sum is %d\n",sum);
return 0;
}
演示如下:
上一个:请问C语言中的\\t在C++中怎么表示啊?
下一个:c语言怎么判断一个变量里面的值是否不为数字啊