如何在我的案例中避免 return null?

How to avoid return null on my case?

我需要编写一个 Java 程序(class 日期)和 "class NextDay that calculates and prints the date of the next day by entering the day, month, and year."

public Date getNextDay()方法中我必须使用return null,否则会报错。如何避免 return null?

这是我的代码;

public class Date {
    private int day;
    private int month;
    private int year;


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


    public int getMaxDaysInMonth()
    {
        int daysInMonth = 0;
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                daysInMonth = 31;
                break;
            case 2:
                if(isLeapYear())
                {
                    daysInMonth = 29;
                }
                else
                {
                    daysInMonth = 28;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                daysInMonth = 30;   
        }

        return daysInMonth;
    }



    public Date getNextDay(){
        try {
            if(day < getMaxDaysInMonth()){
                return new Date(day + 1, month, year);
            }
            else if(day == getMaxDaysInMonth() & month < 12){
                return new Date(1, month+1, year);
            }
            else if(day == getMaxDaysInMonth() & month == 12){
                return new Date(1, 1, year+1);
            }
        } catch (Exception e) {
            System.out.println("You have entered an invalid Date.");
            e.printStackTrace();

        }
        return null;
    }




    public int getDay(){
        return day;
    }

    public int getMonth(){
        return month;
    }

    public int getYear(){
        return year;
    }

    public boolean isLeapYear(){
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

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




public class NextDay {

           public static void main(String args[]) throws Exception{
              Date dateObj = new Date(28, 2, 2015);
              System.out.println("Old Date: " + dateObj.getDay() + "." + dateObj.getMonth() + "." + dateObj.getYear() + ".");
              System.out.println("The next day is " + dateObj.getNextDay().toString() + ".");
           }
}

我建议你 return 一个过去的日期,避免 return 为 null。

使用局部变量并在方法末尾赋值和return,如果可以提高性能,可以在代码中使用跳转语句

public Date getNextDay(){
            Date date = new Date();
            try {
                if(day < getMaxDaysInMonth()){
                    date= new Date(day + 1, month, year);
                }
                else if(day == getMaxDaysInMonth() & month < 12){
                    date = new Date(1, month+1, year);
                }
                else if(day == getMaxDaysInMonth() & month == 12){
                    date = new Date(1, 1, year+1);
                }
            } catch (Exception e) {
                System.out.println("You have entered an invalid Date.");
                e.printStackTrace();

            }
            return date;
        }

我建议抛出异常。作为例外的一部分,您还可以给出一些关于 was/is 错误的解释。

返回特定日期不是一个好主意,因为此日期也可能与以有效方式创建的日期相匹配。 IE。可以毫无问题地创建日期 01-01-1970,对吧?所以它不应该作为问题的种类或标记返回。

关于您的日期表示,请记住您可以使用 Integer.MAX_VALUE 初始化日期的月、日和年。这种情况下的预期输出是什么?