下面我有一个我不会工作的代码片段。我在我的主要方法中获得输入,然后将该输入传递给另一个方法以检查验证。但它并没有真正正确地检查。如果我输入99
formonth
并且day
我希望它给我消息Check Month
。
相反,我得到:
THIS
THIS
如果我输入02
月份和99
日期,我希望它会给我以下信息:Check day
. 相反,我得到THIS THIS
如果我同时输入02
两者,我希望它能够运行并继续运行其他方法。相反,我得到THIS THIS
.
public class Date {
private Calendar parsedDate;
public static void main(String[] args)
{
Date main = new Date();
System.out.println("Enter a date (use the format -> (MM/DD/YYYY)");
//declare Scanner
Scanner in = new Scanner (System.in);
System.out.println("Enter a month (MM): ");
String month = in.nextLine();
System.out.println("Enter a day (DD): ");
String day = in.nextLine();
System.out.println("Enter a year (YYYY): ");
String year = in.nextLine();
if (main.isValidDate(month, day, year) == true)
{
main.newFormat(month, day, year);
main.isLeapYear(year);
main.dayNumber(month, day);
}
else if (main.isValidDate(month, day, year) == false)
{
System.out.println("Invalid Input");
}
}//end of main
private boolean isValidDate(String month, String day, String year)
{
//check month
if(month == "01" || month == "03" || month == "04" ||
month == "05" || month == "06" || month == "07" || month == "08" ||
month == "09" || month == "10" || month == "11" || month == "12")
{
//check day
if(day == "01" || day == "02" || day == "03" || day == "04" ||
day == "05" || day == "06" || day == "07" || day == "08" ||
day == "09" || day == "10" || day == "11" || day == "12" ||
day == "13" || day == "14" || day == "15" || day == "16" ||
day == "17" || day == "18" || day == "19" || day == "20" ||
day == "21" || day == "22" || day == "23" || day == "24" ||
day == "25" || day == "26" || day == "27" || day == "28" ||
day == "29" || day == "30" || day == "31")
{
return true;
}
else
{
System.out.println("Check Day");
return false;
}
}//end of check month
else if (month == "02")
{
if (day == "28" || day == "29")
{
return true;
}
}//end of month 2
else
{
System.out.println("THIS");
return false;
}
parsedDate = null;// if it's valid set the parsed Calendar object up.
return true;
}//end of isValidDate