-1

我想要做的是访问一个对象,在这种情况下 date1 具有 3 个属性日、月和年。我正在尝试创建一个名为 showTomorrow() 的方法,它将以字符串格式显示对象信息 1 天。这意味着我无法更改原始对象的属性。

我已经编写了 Data.java 程序,它如下所示,如果有人能指出我正确的方向或向我展示它会有什么帮助的话。

我相信这就是我在主要方法上运行的内容。

**Date date1 = new Date(30, 12, 2013)** // instantiate a new object with those paramaters

**date1.showDate();** // display the original date

**date1.tomorrow();** // shows what that date would be 1 day infront

问题是现在它没有显示任何内容。我想通过说 dayTomorrow = this.day++; 我将它的默认值 + 1 天添加到变量 dayTomorrow。

public class Date
{
    private int day;
    private int month;
    private int year;
    private int dayTomorrow;
    private int monthTomorrow;
    private int yearTomorrow;

    public Date()
    {
            day = 1;
            month = 1;
            year = 1970;
    }
    public Date(int inDay, int inMonth, int inYear)
    {
            day = inDay;
            month = inMonth;
            year = inYear;
    }
    public void setDate(int inDay, int inMonth, int inYear)
    {
            day = inDay;
            month = inMonth;
            year = inYear;
    }
    public String getDate()
    {
            String strDate;
            strDate = day + "/" + month + "/" + year;
            return strDate;
    }
    public String getTomorrow()
    {
            String strTomorrow;
            strTomorrow = dayTomorrow + "/" + monthTomorrow + "/" + yearTomorrow;
            return strTomorrow;
    }
    public String tomorrow()
    {
            dayTomorrow = this.day++;
            monthTomorrow = this.month;
            yearTomorrow = this.year;

            if(dayTomorrow > 30)
            {
                    dayTomorrow = 1;
                    monthTomorrow = this.month++;
            }
            if(monthTomorrow > 12)
            {
                    monthTomorrow = 1;
                    yearTomorrow = this.year++;
            }

            return getTomorrow();
    }
    public void showDate()
    {
            System.out.print("\n\n THIS OBJECT IS STORING ");
            System.out.print(getDate());
            System.out.print("\n\n");
    }
    public void showTomorrow()
    {
            System.out.print("\n\n THE DATE TOMORROW IS ");
            System.out.print(getTomorrow());
            System.out.print("\n\n");
    }
    public boolean equals(Date inDate)
    {
            if(this.day == inDate.day && this.month == inDate.month && this.year == inDate.year)
            {
                    return true;
            }
            else
            {
                    return false;
            }
    }
}
4

4 回答 4

2

您只需要使用++this.day,++this.month++this.year。当您使用this.day++它时,它会返回以前的日期值,而不是新的。把它++放在前面解决了这个问题。此外,它会更改day值...您可能希望将其更改为this.day + 1.

于 2013-03-04T19:54:50.500 回答
0

看看这个:http ://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

增量后...

于 2013-03-04T20:08:46.937 回答
0

您可以在 java 中使用本机日期支持,但我认为您只是在练习,对吗?

这应该可以解决问题:

public class Date {
    private int day = 1;
    private int month = 1;
    private int year = 1970;
    private int dayTomorrow = day+1;
    private int monthTomorrow;
    private int yearTomorrow;

    public Date()
    {
            tomorrow();
    }
    public Date(int inDay, int inMonth, int inYear)
    {
            day = inDay;
            month = inMonth;
            year = inYear;
            tomorrow();
    }
    public void setDate(int inDay, int inMonth, int inYear)
    {
            day = inDay;
            month = inMonth;
            year = inYear;
    }
    public String getDate()
    {
            String strDate;
            strDate = day + "/" + month + "/" + year;
            return strDate;
    }
    public String getTomorrow()
    {
            String strTomorrow;
            strTomorrow = dayTomorrow + "/" + monthTomorrow + "/" + yearTomorrow;
            return strTomorrow;
    }
    public void tomorrow()
    {
            monthTomorrow = this.month;
            yearTomorrow = this.year;

            if(dayTomorrow > 30)
            {
                    dayTomorrow = 1;
                    monthTomorrow = this.month++;
            }
            if(monthTomorrow > 12)
            {
                    monthTomorrow = 1;
                    yearTomorrow = this.year++;
            }
    }
    public void showDate()
    {
            System.out.print("\n\n THIS OBJECT IS STORING ");
            System.out.print(getDate());
            System.out.print("\n\n");
    }
    public void showTomorrow()
    {
            System.out.print("\n\n THE DATE TOMORROW IS ");
            System.out.print(getTomorrow());
            System.out.print("\n\n");
    }
    public boolean equals(Date inDate)
    {
            if(this.day == inDate.day && this.month == inDate.month && this.year == inDate.year)
            {
                    return true;
            }
            else
            {
                    return false;
            }
    }
}

仔细查看我所做的任何更改;)这是主要的:

public static void main(String[] args) {
    Date d = new Date();
    d.showDate();
    d.showTomorrow();
}
于 2013-03-04T20:12:47.987 回答
0

您是否在 date1.tomorrow() 之后调用 showDate() 来显示您的输出?

或代替date1.tomorrow();打电话date1.showTomorrow();

于 2013-03-04T20:01:07.967 回答