public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the month (1-12):");
int month = input.nextInt();
System.out.print("Enter the day (1-31):");
int day = input.nextInt();
System.out.print("Enter the year (ex. 2014):");
int year = input.nextInt();
if (month == 1)
month = 13;
else if (month == 2)
month = 14;
int Zeller = day + ((26 * (month + 1))/10) + year +
(year/4) + (6 * (year/100)) + (year/400);
int dayofweek = (Zeller + 5) % 7 + 1;
if (dayofweek == 1)
System.out.print("Day of the week: Monday");
else if (dayofweek == 2)
System.out.print("Day of the week: Tuesday");
else if (dayofweek == 3)
System.out.print("Day of the week: Wednesday");
else if (dayofweek == 4)
System.out.print("Day of the week: Thursday");
else if (dayofweek == 5)
System.out.print("Day of the week: Friday");
else if (dayofweek == 6)
System.out.print("Day of the week: Saturday");
else if (dayofweek == 7)
System.out.print("Day of the week: Sunday");
}
該代碼將不會正確計算1月和2月的日期。它總是一兩天。澤勒的同餘代碼錯誤
例如:如果我輸入Jan 1, 2010
,代碼應該輸出Day of the week: Friday
,但相反,它說它的Day of the week: Saturday
。
我檢查了我的計算結果找不到任何錯誤。
我害怕的'年/ 100','年/ 400',和其他整數部門。 – 2014-09-22 18:19:00
我應該在這裏做什麼?宣佈一個新的整數? – Michael 2014-09-22 18:23:33
當你說你已經「檢查了你的計算」,你在做整數除法嗎?如果這個術語對你來說沒有任何意義,那很可能是你的bug的根源。 – dimo414 2014-09-22 18:27:14