I tried to run this with a input of 02/29/2001, which ideally should print out the message of You entered an invalid date, but instead, it just terminated after finishing running the program. Where did it go wrong?
Scanner keyboard = new Scanner(System.in);
String date;
String mm, dd, yyyy;
System.out.print("Please enter a date in mm/dd/yyyy format: ");
date= keyboard.nextLine();
mm = date.substring(0, 2);
dd = date.substring(3,5);
yyyy = date.substring(6,10);
int year = Integer.parseInt(yyyy);
int day = Integer.parseInt(dd);
int month = Integer.parseInt(mm);
boolean isLeapYear=false;
if (year % 4 ==0)
{
if (year % 100 !=0)
isLeapYear=true;
else if (year % 4 ==0 && year % 100 ==0)
{ if (year % 400 == 0)
isLeapYear=true;
}
else
isLeapYear=false;
}
if (isLeapYear=false)
{
if(month==2 && day>28)
System.out.println("You entered an invalid date.");
}
Aucun commentaire:
Enregistrer un commentaire