為什么會多出4天來?請教大神給我找找錯吧!
#include <stdio.h>
int main()?
{?
? ? /* 定義需要計算的日期 */
? ? int year = 2008;
? ? int month = 8;
? ? int day = 8;
? ??int i;
? int ?sum=0;
??for(i=1;i<month;i++){ ??
? ?if(month==2){
? ?if(year%4==0&&year%100!=0||year%400==0)?
? ?{
? ? ? ? ? ?sum+=29;
? ? ? ?}
? ? ? ?else
? ? ? ?{
? ? ? ? ? ?sum+=28;
? ? ? ?}
? ?}else?
? ?{
? ?if(month==4||month==6||month==9||month==11){
? ? ? ?sum+=30;
? ?}else{
? ? ? ?sum+=31;
? ?} ??
? ?}
}sum+=day;
? ?printf("%d年%d月%d日是今年的第%d天",year,month,day,sum);
return 0;
}?
2015-07-03
一個明顯的問題是sum沒賦初值;
另外一個不合理的小問題是sum+=day盡量放到for循環(huán)體之前,你這里month=8的話這樣寫還沒問題,如果month=1呢