2011-11-01 56 views
0

我得到的錯誤是「System.ArgumentOutOfRangeException was unhandled。」它要求月份在1和12之間。但是看看調試器中的變量,它等於1以及一個Debug.Writeline。循環中的歧義?

int month, year, total; 
    total = 0; 
    DateTime dayToFind; 

    for (year = 1001; year < 1201; year++){ 
     for (month = 1; month < 12; month++){ 
      dayToFind = new DateTime(year, month, DateTime.DaysInMonth(month, year)); 
      // The error points at the last occurance of month above. 
      total = (dayToFind.DayOfWeek == DayOfWeek.Monday) ? 1 : 0; 
     } 

    } 
+1

不應該是'month = 1;月<13歲,或者你故意跳過12月? –

+0

是的,當我切換機器時,我輸入的太快而不是僅僅複製和粘貼。另請注意'total ='應該是'total + ='。除非我喜歡循環而不是實際聚合。 – surfasb

回答

8

你有你的DateTime.DaysInMonth()回調。它改成這樣:

DateTime.DaysInMonth(year, month) 

當你把year變量的month的地方,它是大於最大,它可以是(大於12),導致ArgumentOutOfRangeException

DateTime.DaysInMonth()

+0

Ack。去搞清楚。 – surfasb

+0

@surfasb :)它發生在我們所有人身上。沒有什麼比看着它的額外眼睛更好。 – 2011-11-01 20:10:30

+0

有一首歌,「我得到朋友的一點幫助......」 – surfasb

2

你扭轉了論據,DaysInMonth方法。年份第一。