2014-09-22 4 views
2
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이 출력됩니다.

제 계산에서 오류를 찾을 수 없습니다.

+2

내가 년/100',''무서워 년/400' 및 기타 정수 구분. –

+0

여기서 어떻게해야합니까? 새로운 인터 사이거 야? – Michael

+1

정수 나누기를 할 때 "계산을 점검했다"고 말할 때? 그 용어가 당신에게 아무런 의미가 없다면 그것은 아마도 당신의 버그의 근원 일 것입니다. – dimo414

답변

5

Zeller's congruence wiki page에 따르면 1 월과 2 월 개월을 조정할 때 알고리즘은 1 월과 2 월이 전년도의 "13 번째"및 "14 번째"달로 간주되기 때문에 연도를 조정해야합니다.

그러나 컴퓨터를 사용하는 경우, Y입니다 수정 해 Y, 처리 간단 - 1 월과 2 월 동안 1 :

이 1 월과 2 월의 경우에 올해 조정 코드를 추가합니다. 1 월 1 일의 테스트 케이스

if (month == 1) 
{ 
    month = 13; 
    year--; // add this 
} 
else if (month == 2) 
{ 
    month = 14; 
    year--; // add this 
} 

출력 일 :

Enter the month (1-12):1 
Enter the day (1-31):1 
Enter the year (ex. 2014):2010 
Day of the week: Friday 

사례는 위의 단순화 할 수있다 :

if (month == 1 || month == 2) 
{ 
    month += 12; 
    year--; 
} 
+0

2008 년 2 월 29 일 2015 년 1 월 17 일 1776 년 7 월 4 일 해당 날짜는 여전히 생산되지 않습니다. – Michael

+0

2008 년 2 월 29 일 금요일이었습니다. 2015 년 1 월 17 일 토요일이됩니다. 1776 년 7 월 4 일 목요일이었다. 내 변경 사항과 함께 귀하의 프로그램 복사본이 올바른 것입니다. – rgettman

+0

나는 그 변화를 만들었지 만 여전히 나에게 적절한 날짜를주지 않을 것이다. – Michael