【问题标题】:How to avoid return null on my case?如何避免在我的情况下返回 null?
【发布时间】:2016-10-16 15:08:48
【问题描述】:

我需要编写一个Java程序(Date类)和“通过输入日、月、年计算并打印第二天的日期的NextDay类。”

public Date getNextDay() 方法我必须使用 return null,否则会报错。如何避免返回 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() + ".");
           }
}

【问题讨论】:

  • 你可以返回一个新的日期对象,它是 01-01-1970
  • 可以,但是,这是一个好的解决方案吗?
  • 请记住,使用您的课程的开发人员必须遵循您提供的文档,因此如果您告诉他,如果提供了无效日期,则该方法返回 null 或返回日期 1970,并没有改变太多,这只是一种练习

标签: java null switch-statement try-catch try-catch-finally


【解决方案1】:

使用局部变量并赋值并在方法结束时返回,如果可以提高性能,您可以在代码中使用跳转语句

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;
        }

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-27
        相关资源
        最近更新 更多