Java 日历实现:添加日期的问题

Java 日历实现:添加日期的问题,java,Java,我的任务是: 实现将日期移动一天的方法public void advance()。在本练习中,我们假设每个月有30天。注意!在某些情况下,您需要更改月份和年份的值 我已经这样做了: public class SimpleDate { private int day; private int month; private int year; public SimpleDate(int day, int month, int year) { thi

我的任务是: 实现将日期移动一天的方法public void advance()。在本练习中,我们假设每个月有30天。注意!在某些情况下,您需要更改月份和年份的值

我已经这样做了:

public class SimpleDate {

    private int day;
    private int month;
    private int year;

    public SimpleDate(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    @Override
    public String toString() {
        return this.day + "." + this.month + "." + this.year;
    }

    public boolean before(SimpleDate compared) {
        if (this.year < compared.year) {
            return true;
        }

        if (this.year == compared.year && this.month < compared.month) {
            return true;
        }

        if (this.year == compared.year && this.month == compared.month &&
                 this.day < compared.day) {
            return true;
        }

        return false;
    }

    public void advance(){
        if(this.day<30){
            this.day += 1;
        }    
        if(this.day==30 && this.month==12){
            this.day = 1;
            this.month=1;
            this.year +=1;
        }
        if(this.day==30){
            this.day = 1;
            this.month +=1;
        }
    }

    public void advance(int howManyDays){
        if(howManyDays < 30){
            if(this.day<30 && (this.day + howManyDays < 30)){
                this.day += howManyDays;
            }
            if(this.day==30 && this.month==12){
                this.day = howManyDays;
                advance();
            }
            if(this.day==30){
                this.day = howManyDays;
                this.month +=1;
            } 
        }
        if(howManyDays== 30 && this.month==12){
            this.day = howManyDays;
            advance();
        }
        if(howManyDays == 30){
            this.month += 1;
        }
    }
    public SimpleDate afterNumberOfDays(int days){

        SimpleDate obj = new SimpleDate(days, this.month,this.year);
        return obj;

    }

}
现在日期应该是2012年1月3日。
应为:
[3.1.2012]
但为:
[4.12.2011]
发生这种情况是因为您设置了日期,然后调用了
advance()
。因此,这一天改为
3
,但现在调用您的
advance
,日期为
2011年12月3日
,结果是
2011年12月4日

您可以通过两种方式解决此问题:

低效途径 只需拨打
advance
多少天
次:

public void advance(int howManyDays){
    for(int i = 1; i <= howManyDays; i++)
        advance();
}
public void advance(整数多少天){

对于(int i=1;i我建议您在这个问题上使用
while
循环来简化您的方法,而不是使用多个嵌套的
if
语句

public void advance(int howManyDays) {
  this.day += howManyDays;

  //Subtracts out the days till within range, incrementing months
  while(this.day > 30) {
    this.day -= 30;
    this.month++;
  }

  //Subtracts out the months till within range, incrementing years
  while(this.month > 12) {
    this.month -= 12;
    this.year++;
  }
}

在您的测试用例中,您通过了以下程序块:

        if(this.day==30 && this.month==12){
            this.day = howManyDays;
            advance();
        }
进入此块时,您有
天==30
月==12
多少天==3

  • 您将day设置为
    howManyDays
    ,因此
    现在day==3
  • 调用
    advance()
    ,因为day是3,day是递增的,然后
    advance()
    方法返回
  • 没有其他有效的
    如果
    条件,
    advance(3)
    方法返回
只有
day
字段已更改

在设置日期之前调用
advance()
,它应该可以工作

        if(this.day==30 && this.month==12){
            advance(); // go to the next day, change the month/year if necessary
            this.day = howManyDays-1; // advance() already goes one day forward
        }
您的代码还有其他问题,但我会让您找到并修复它们。(例如:如果
是27天,而
多少天
是3天怎么办?)


我强烈建议您学习使用分步调试器来处理这些简单的逻辑问题。这是一项很有价值和非常有用的技能。

您应该使用分步调试器来了解您的程序的功能。它完全按照您的编程来做:日期是2011年12月30日;
this.day==30&&this.month==12
,因此它设置了
this.d日期
多少天
(日期现在是2011年12月3日),您本月没有触摸
(日期仍然是2011年3月12日),然后您调用了
advance()
(日期现在是2011年12月4日)。
        if(this.day==30 && this.month==12){
            this.day = howManyDays;
            advance();
        }
        if(this.day==30 && this.month==12){
            advance(); // go to the next day, change the month/year if necessary
            this.day = howManyDays-1; // advance() already goes one day forward
        }