我有一个检查输入日期的月,日,年是否正确的方法,该方法正常工作,但如果输入的日期月/年/日无效,则打印错误消息每个检查(月/日/年)都是错误的。我只有一条错误信息,而不是全部三条。
这是我所拥有的方法的代码:
//Check to see if the date is valid
public static boolean isValid(int day, int month, int year){
boolean monthValid, yearValid, dayValid;
boolean check = true;
int dayNumber = 0;
//check to see if the month is valid
if(month <= 12 && month > 0){
monthValid = true;
}else{
monthValid = false;
System.out.println("Invalid month.");
check = false;
}
//Check to see if the year is valid
if (year <= 3000 && year >= 1){
yearValid = true;
}else{
yearValid = false;
System.out.println("Invalid Year.");
check = false;
}
//assigns month to a number
if (month == JAN || month == MAR || month == MAY || month == JUL || month == AUG ||month == OCT || month == DEC){
dayNumber = LONG;
}else if (month == APR || month == JUN || month == SEP || month == NOV){
dayNumber = SHORT;
}else if (month == FEB && true == isLeapYear(year)){
dayNumber = AKWARD;
}else if( month == FEB){
dayNumber = MINI;
}
//check to see if day is valid
if (day < dayNumber && day > 0 || day == dayNumber){
dayValid = true;
}else if(day == AKWARD && false == isLeapYear(year)){
System.out.println("Invalid day number for February for given year.");
check = false;
}else if( day > dayNumber || day < 0){
dayValid = false;
System.out.println("Invalid day number for month.");
check = false;
}
return check;
}
所以如果输入的日期是13/34/3001(MM / DD / YYYY) 输出是:
以三种数字格式输入日期,m / d / year:13/34/3001
月份无效。
年度无效。
月份的天数无效。
同样,我只需要在这三个中打印一条错误消息。
谢谢!
答案 0 :(得分:0)
不是设置check = false,而是在写入错误消息时立即返回false:
yearValid = false;
System.out.println("Invalid Year.");
return false;
答案 1 :(得分:0)
这最像是一个设计问题。它可以有多种解决方案。
Simple返回false,打印出第一条错误信息后就不会再进一步了。
否则使用stringbuffer,将所有错误消息添加到一个并打印出来。你的选择。
答案 2 :(得分:0)
您可以使用例外兄弟。
将您的检查代码包装在try / catch中,并在出现错误时抛出异常。像这样:
//Check to see if the date is valid
public static boolean isValid(int day, int month, int year){
boolean monthValid, yearValid, dayValid;
boolean check = true;
int dayNumber = 0;
try{
//check to see if the month is valid
if(month <= 12 && month > 0){
monthValid = true;
}else{
monthValid = false;
throw new IllegalArgumentException("INVALID");
}
//Check to see if the year is valid
if (year <= 3000 && year >= 1){
yearValid = true;
}else{
yearValid = false;
throw new IllegalArgumentException("INVALID");
}
//assigns month to a number
if (month == JAN || month == MAR || month == MAY || month == JUL || month == AUG ||month == OCT || month == DEC){
dayNumber = LONG;
}else if (month == APR || month == JUN || month == SEP || month == NOV){
dayNumber = SHORT;
}else if (month == FEB && true == isLeapYear(year)){
dayNumber = AKWARD;
}else if( month == FEB){
dayNumber = MINI;
}
//check to see if day is valid
if (day < dayNumber && day > 0 || day == dayNumber){
dayValid = true;
}else if(day == AKWARD && false == isLeapYear(year)){
throw new IllegalArgumentException("INVALID");
}else if( day > dayNumber || day < 0){
dayValid = false;
throw new IllegalArgumentException("INVALID");
}
}catch(IllegalArgumentException e)
{
System.out.println("Invalid date.");
return false;
}
return true;
}