我一直在网上寻找解决方案,但没有 运气被发现了。所以我去堆栈溢出看到一些解决方案,但仍然没有运气,所以我发布我的代码分享,并帮我解决这个问题。感谢你们!
public class MyDate {
private int month;
private int day;
private int year;
private String MoInTxt;
private int dayz;
public MyDate(){
this(1, 1, 1990);
}
public MyDate(int d, int m, int y){
setDay(d);
setMonth(m);
setYear(y);
}
// Setter
public String MonthToTxt() {
int mo = month;
switch (mo) {
case 1: MoInTxt = "January";
break;
case 2: MoInTxt = "February";
break;
case 3: MoInTxt = "March";
break;
case 4: MoInTxt = "April";
break;
case 5: MoInTxt = "May";
break;
case 6: MoInTxt = "June";
break;
case 7: MoInTxt = "July";
break;
case 8: MoInTxt = "August";
break;
case 9: MoInTxt = "September";
break;
case 10: MoInTxt = "October";
break;
case 11: MoInTxt = "November";
break;
case 12: MoInTxt = "December";
break;
default: MoInTxt = "Invalid month";
break;
}
}
public void setMonth(int m) {
if(m>=1 && m <=12) {
month = m;
if(m ==2 && LeapChecker(year) == true) {
month = 29;
}else{
month = 28;
}
}else {
month = 1;
}
}
public void setDay(int d) {
if(day>0) {
day = d;
}else{
day = 1;
}
public void setYear(int yr) {
if (yr >=1900){
year = yr;
} else {
year = 1900;
}
}
// Getter
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() {
return year ;
}
//==============================//
public String getCompleteDate(){
return String.format ("%s %02d, %d", MoInTxt, day, year);
}
//==============================//
public void addDays(int dey) {
int addedDey = dey + day;
int ToAddMos=0;
if(addedDey <=28) {
setDay(addedDey);
} else {
while(addedDey>28){
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month == 12){
if(addedDey>31){
addedDey=addedDey-31;
setDay(addedDey);
ToAddMos++;
}else{
setDay(addedDey);
break;
}
}else if(month ==2) {
if(LeapChecker(year) == true){
if(addedDey >29) {
addedDey = addedDey - 29;
ToAddMos++;
}else {
setDay(addedDey);
break;
}
}else if(LeapChecker(year) == false) {
if(a>28){
addedDey = addedDey - 28;
ToAddMos++;
}else{
setDay(AddedDey);
break;
}
}
}else if(month == 4 || month == 6 || month == 9 || month == 11){
if(addedDey>30){
addedDey-=30;
ToAddMos++;
} else {
setDay(addedDey);
break;
}
}
}
}
}
public void addMonths(int mon) {
int addedMon = mon + month;
setMonth(addedMon);
}
public void addYears(int yir) {
int addedYir = year + yir;
setYear(addedYir);
}
/* Method Name: Leap Checker
* This Method checks if that year is a leap year or not
* 1. It checks if the year is divisible by 400
* 2. It checks if the year is divisible by 4
* 3. It checks if the year is not divisible by 100
*/
public boolean LeapChecker(int year){
year = year;
boolean isLeap =true;
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
isLeap = true;
} else {
isLeap = false;
}
return isLeap;
}
}
}
答案 0 :(得分:2)
public void setDay(int d) {
if(day>0) {
day = d;
}else{
day = 1;
}//Missing closing the else block
}
答案 1 :(得分:2)
问题是在setDay()
方法结束时缺少结束大括号:
public void setDay(int d) {
if(day>0) {
day = d;
}else{
day = 1;
} // <------------------ this one
}