日期:2014-05-20  浏览次数:20757 次

计算下个有相同星期的闰年是那一年,为什么输入1896的时候显示的不对呢?
[code=Java][/code]
import java.util.Scanner;
public class LeapYear{
public static void main(String[] args){
System.out.println("Please enter a year greater than 1582.");
Scanner scan=new Scanner(System.in);
int year=scan.nextInt();
int month=2,day=29;
if(year>=1582){
if(isLeapYear(year)){
System.out.println("\n"+year+" is leap year");
Zellers(year,month,day);
}
else{
  System.out.println("\n"+year+" is not leap year");
}
}
else{
System.out.println("\nThe year that entered is out of range.");
}
}
public static boolean isLeapYear(int year){
if (year%4==0 && year%100!=0){
return true;
}
else{
if (year%400==0)
return true;
}
return false;
}
public static void Zellers(int year,int month,int day){
int LeapFactor,DayNumber,StartMonth,StartYear;
if (month<3)
{
StartMonth=0;
StartYear=year-1;
}
else 
{
StartMonth = (int) (0.4*month+2.3);
StartYear = year;
}
LeapFactor =(StartYear/4)-(StartYear/100)+(StartYear/400);
DayNumber=((365*year+31*(month-1)+day+LeapFactor-StartMonth)-1)%7;
System.out.println("\n"+DisplayDay(DayNumber)+ " was Feb 29th in "+year);
for (int year1=year; year1>1582; year1++)
{
int DayNumber1=((365*year1+31*(month-1)+day+LeapFactor-StartMonth)-1)%7;
if(isLeapYear(year1) && DayNumber1==DayNumber)
{
System.out.println("\n"+year1+" is the next leap year where Feb. 29th happens on "+DisplayDay(DayNumber));
System.out.println("\n**********************************END************************************");
return;
}
}
  }
public static String DisplayDay(int DayNumber){
switch (DayNumber)
{
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
default:
return "";
}
}
}


------解决方案--------------------
1986年貌似不是闰年吧

------解决方案--------------------
看错,是1896。

试了哈,是正确的,没有问题啊