所以,我正在尝试编写一个程序,它以“mmddyyyy”格式作为输入并返回下一个回文日期(实际上它返回年份,可以反转并预先附加到自身)。以下是我的代码:它给出了正确的结果“我所知道的远近”。现在,我觉得代码既错误又有逻辑漏洞。忽略了我严格遵守“mmddyyyy”输入格式(以及一些硬编码)这一事实,您认为代码缺失了什么?
public class NextPalindromeYear {
// Date in mmddyyyy format
//12032014
public int nextPalindromeYear(String date) {
char[] dateArr = date.toCharArray();
int[] dateIntArr = new int[dateArr.length];
int[] numberOfDays = {31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=0; i < dateArr.length;i++) {
dateIntArr[i] = dateArr[i] - '0';
}
int month = dateIntArr[0] * 10 + dateIntArr[1];
int day = dateIntArr[2] * 10 + dateIntArr[3];
int year = 0;int pow = 1;
for(int i= 7; i > 3; i--) {
year += dateIntArr[i] * pow ;
pow *= 10;
}
int givenYear = year;
System.out.println("Month is " + month);
System.out.println("Date is " + day);
while(true) {
System.out.println("Year is " + year);
int split1 = year / 100;
int split2 = year % 100;
split1 = ((split1 % 10) * 10 ) + (split1/10);
split2 = ((split2 % 10) * 10 ) + (split2/10);
System.out.println("Split 1 is " + split1);
System.out.println("Split 2 is " + split2);
if(split1 >31 || split2 > 11) {
year++;
continue;
}
if(year%4 == 0) {
numberOfDays[1] = 29;
}
else {
numberOfDays[1] = 28;
}
if(numberOfDays[split2] >= split1) {
if(year == givenYear) {
if(!(split1 >= day && split2 > month)) {
year++;
continue;
}
}
return year;
}
year++;
}
}
}