所以我一直在研究這個程序,並且我已經得到了一些日期來解決問題,但是當我輸入未來一年的二月份時,它表明它的無效當它不應該。 爲了這個節目在2月的日子只是1-28。 任何人都可以幫我抓住我的錯誤嗎?將2月份的問題顯示爲有效日期
import java.util.Scanner;
public class FutureDate {
public static void main (String args[]){
int inputMonth;
int inputDate;
int inputYear;
final int currentMonth = 10;
final int currentDate = 24;
final int currentYear = 2013;
final int numberOfDays = 31;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a month in format - mm: ");
inputMonth = keyboard.nextInt();
System.out.println("Enter a day in format - dd: ");
inputDate= keyboard.nextInt();
System.out.println("Enter a year in format - yyyy: ");
inputYear = keyboard.nextInt();
if (inputYear < currentYear)
System.out.println("Invalid Date");
else if (inputYear >= currentYear)
{
if (inputMonth >= 1 && inputMonth <=12){
if ((inputMonth == 4 || inputMonth == 6 || inputMonth == 9 || inputMonth == 11) && (inputDate >= 1 && inputDate <= 30))
System.out.println("Valid Date");
else if (inputMonth > currentMonth && inputDate >= 1 && inputDate <=30)
System.out.println("Valid Date");
else if (inputMonth == currentMonth && inputDate > currentDate && inputDate <=30)
System.out.println("Valid Date");
else
System.out.println("Invalid Date");
}
else if (inputMonth == 2 && inputDate >= 1 && inputDate <= 28){
System.out.println("Valid Date");
if (inputMonth > currentMonth && inputDate >= 1 && inputDate <=28)
System.out.println("Valid Date");
else if (inputMonth == currentMonth && inputDate > currentDate && inputDate<=28)
System.out.println("Valid Date");
else
System.out.println("Invalid Date");
}
else if (inputMonth == 1 || inputMonth == 3 || inputMonth == 5 || inputMonth == 7 || inputMonth ==10 || inputMonth == 12 && inputDate >= 1 && inputDate <= 31){
if (inputMonth > currentMonth && inputDate >= 1 && inputDate <=31)
System.out.println("Valid Date");
else if (inputMonth == currentMonth && inputDate > currentDate && inputDate <=31)
System.out.println("Valid Date");
else
System.out.println("Invalid Date");
}
else
System.out.println ("Invalid Date");
}
}
}
如果你打算在某些地方做而不是其他人,那麼總是圍繞着你的else/if塊。使它成爲PITA來閱讀。 –