Java 如何避免在我的案例中返回null?
我需要编写一个Java程序(class Date)和“class NextDay,它通过输入日期、月份和年份来计算和打印第二天的日期。” 在Java 如何避免在我的案例中返回null?,java,null,switch-statement,try-catch,try-catch-finally,Java,Null,Switch Statement,Try Catch,Try Catch Finally,我需要编写一个Java程序(class Date)和“class NextDay,它通过输入日期、月份和年份来计算和打印第二天的日期。” 在public Date getNextDay()方法中,我必须使用return null,否则会产生错误。如何避免返回null 这是我的密码 public class Date { private int day; private int month; private int year; public Date(int d
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() + ".");
}
}
公开课日期{
私人国际日;
私人整数月;
私人国际年;
公开日期(整数天、整数月、整数年){
this.day=天;
本月=月;
今年=年;
}
public int getMaxDaysInMonth()
{
int daysInMonth=0;
开关(月)
{
案例1:
案例3:
案例5:
案例7:
案例8:
案例10:
案例12:
daysInMonth=31;
打破
案例2:
如果(isLeapYear())
{
daysInMonth=29;
}
其他的
{
daysInMonth=28;
}
打破
案例4:
案例6:
案例9:
案例11:
daysInMonth=30;
}
每月的返回日;
}
公开日期getNextDay(){
试一试{
如果(天
我建议您返回过去的日期,避免返回null。使用局部变量并赋值,然后在方法末尾返回,如果可以提高性能,您可以在代码中使用跳跃语句
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;
}
public Date getNextDay(){
日期=新日期();
试一试{
如果(天
我建议抛出一个异常。作为例外情况的一部分,您还可以对过去/现在的错误做出一些解释
返回一个特定的日期不是一个好主意,因为这个日期也可以匹配以有效方式创建的日期。也就是说,1970年1月1日可以毫无问题地创建,对吗?因此,它不应该作为问题的种类或标记返回
关于日期表示,请记住,可以使用Integer.MAX_值初始化月份、日期和年份的日期。这种情况下的预期输出是什么?您可以返回一个新的日期对象,该对象的值为01-01-1970是的,我可以,但是,这是一个好的解决方案吗?请记住,使用You类的开发人员必须遵循您提供的文档,因此如果您告诉他如果提供了无效的日期,该方法将返回null,或者返回日期1970,不会改变太多,这只是一种练习