-3
首先,我知道你們不喜歡這樣的問題,但是我無法看到我的代碼出了什麼問題,並希望有人查看它。據說,我知道我的代碼看起來很糟糕,因爲這必須全部用一種方法完成。Java分配,有缺陷的邏輯
public static int dayOfWeek(int year, int month, int day) {
int monthCode = 0, centuryCode = 0, numLeapYears = 0, dayOfWeek = 0;
int yearDigits = year % 100;
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
numLeapYears = yearDigits/2 + 1;
}else {
numLeapYears = yearDigits/2;
}
if(((year >= 1400) && (year <= 1499)) || ((year >= 1800) && (year <= 1899)) || ((year >= 2200) && (year <= 2299))) {
centuryCode = 2;
}else if(((year >= 1500) && (year <= 1599)) || ((year >= 1900) && (year <= 1999)) || ((year >= 2300) && (year <= 2399))) {
centuryCode = 0;
}else if(((year >= 1600) && (year <= 1699)) || ((year >= 2000) && (year <= 2099)) || ((year >= 2400) && (year <= 2499))) {
centuryCode = 5;
}else if(((year >= 1700) && (year <= 1799)) || ((year >= 2100) && (year <= 2199)) || ((year >= 2500) && (year <= 2599))) {
centuryCode = 4;
}
switch(month) {
case 1: monthCode = 0;
break;
case 2: monthCode = 3;
break;
case 3: monthCode = 3;
break;
case 4: monthCode = 6;
break;
case 5: monthCode = 1;
break;
case 6: monthCode = 4;
break;
case 7: monthCode = 6;
break;
case 8: monthCode = 2;
break;
case 9: monthCode = 5;
break;
case 10: monthCode = 0;
break;
case 11: monthCode = 3;
break;
case 12: monthCode = 5;
break;
}
dayOfWeek = ((centuryCode + yearDigits + numLeapYears + monthCode + day) % 7);
return dayOfWeek;
}//dayOfWeek
我沒有打擾評論我的代碼還沒有,這個功能的目的是計算一個數字(0,1表示這一天落在日(星期日,星期一,等等) ,2等)我的代碼工作,但它不輸出正確的數字。例如7/20/1969應該是0,但是我的代碼輸出爲3.我似乎無法找到這種方法的邏輯缺陷。
numLeapYears應該代表什麼?自今天的日期以來發生了多少次閏年?爲什麼你是yearDigits/2? – Scott
你怎麼決定你的'centuryCode'和'monthCode'? –
numLeapYears應該是本世紀初以來的閏年數。 編輯:這是我的問題,感謝幫助我意識到這一點。 – user123