0
我是一個嘗試製作日曆(功課的一部分)的新手程序員。我已經完成了其他所有事情,但是在我找到新年指數(從0到6)的部分卡住了,除了在閏年之後的一年之後,每年向右移動一次,其中兩次都是這樣。我對這個特定的部分沒有很好的算法,所以我只是想到了我的頭頂。它計算和工作到1800年,但在此之後它不能正常工作了。我不知道爲什麼。但這裏的代碼:查找年度第一天(新年)的索引
公共靜態INT indexOfNewYear(INT月,年整型){
int count = 0;
int modOfDays = 0;
int numberOfDays = 365;
year = year - 2; // starts with year 2 because index of New Year is 0
for (int t = 1; t <= year; year--){
if(year + 2 == 1753){ // year 1752 skipped 11 days ahead
numberOfDays += 10;
}
count = modOfDays + numberOfDays;
modOfDays = count % 7;
if((year + 1) % 4 == 0 && year != 1){ // leap year skips two indexes
modOfDays += 1;
}
}
//System.out.println("modOfDays: " + modOfDays);
return modOfDays;
}
任何幫助,非常感謝!
你知道你的閏年測試不完全正確嗎? –
https://en.wikipedia.org/wiki/Leap_year#Algorithm –
在'for'循環中初始化一個變量並不會改變其值。 'for(int t = 1; ...)' - > t總是1。 – DodgyCodeException