2013-03-08 43 views
0

我必須保持程序基本,不要太花哨。以下是我的問題: 當我輸入日期「2013年11月31日」時,它返回「30天正常月份的錯誤日期輸入」。我明白這意味着我的If/Else語句和/或我的布爾表達式的結構有問題。但我似乎無法弄清楚這個問題。任何幫助表示讚賞,謝謝。在Java中使用我的Date Validation程序獲取不正確的結果

這裏是我的程序:

import java.util.Scanner; 

public class DateValidation { 

public static void main(String[] args) { 

    Scanner keyboard = new Scanner(System.in); 
    String date; 
    System.out.println("Please enter a date in the following format: mm/dd/yyyy "); 
    date = keyboard.next(); 

    int month = Integer.parseInt(date.substring(0, 2)); 
    int day = Integer.parseInt(date.substring(3, 5)); 
    int year = Integer.parseInt(date.substring(6, 10)); 
    boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)); 
    boolean thirtyMonth = (month == 9 || month == 4 || month == 5 || month == 10); 
    boolean regMonth = (month == 1 || month == 3 || month == 6 || month == 7 || month == 8 || month == 11 || month == 12); 

    if ((month <= 0) || (day <= 0) || (day >= 32)) { 
     System.out.println("You entered an incorrect month or day value"); 
    } 
    if ((month == 2) && (isLeapYear == true) && (day > 0) && (day <= 29)) { 
     System.out.println("it's a leap year - the date is " + date); 
     System.exit(0); 
    } else if ((month == 2) && (isLeapYear == false) && (day > 0) && (day <= 28)) { 
     System.out.println("it's not a leap year - the date is " + date); 
     System.exit(0); 
    } else if (month == 2) { 
     System.out.println("Your input is invald. Non leap-years only have 28 days"); 
     System.exit(0); 
    } 

    if ((thirtyMonth == true) && (day > 0) && (day <= 30)) { 
     System.out.println("This is a valid date for a 30 day month - here is the date " + date); 
     System.exit(0); 
    } else if ((thirtyMonth == true) && (day < 0) || (day >= 31)) { 
     System.out.println("Incorrect day input for a regular month with 30 days"); 
     System.exit(0); 
    } 

    if ((regMonth) && (day > 0) && (day <= 31)) { 
     System.out.println("This is a valid date for a regular month with 31 days - here is the date " + date); 
     System.exit(0); 
    } else if ((regMonth) && (day < 0) && (day >= 32)) { 
     System.out.println("Incorrect day input for a regular month with 31 days"); 
     System.exit(0); 
    } 


} 

}

+0

你可能想嘗試[喬達](http://joda-time.sourceforge.net/)。另外,你爲什麼這樣做? – Jess 2013-03-08 02:43:17

+0

不使用if(isLeapYear == true),使用if(isLeapYear)。同樣,使用if(!isLeapYear)代替if(isLeapYear == false) – ncmathsadist 2013-03-08 02:44:15

+0

它有助於將值放入輸出消息中,例如System.out.println(day +「在30天內一個月內無效」); System.exit(0); – BevynQ 2013-03-08 02:51:50

回答

2

四月,六月和九月是十一月30天月

boolean thirtyMonth = (month == 9 || month == 4 || month == 5 || month == 10); 

應該

boolean thirtyMonth = (month == 9 || month == 4 || month == 6 || month == 11); 
boolean regMonth = (month == 1 || month == 3 || month == 5 ||month == 7 || month == 8 || month == 10 || month == 12); 
+0

啊我明白了!我想這就是所謂的「邏輯錯誤」謝謝 – bytebybyte 2013-03-08 02:54:48

+0

@bytebybyte,你也需要看看Evgeniy Dorofeev的回答。 – Jess 2013-03-08 03:26:25

1

變化

} else if ((thirtyMonth == true) && (day < 0) || (day >= 31)) { 

} else if (thirtyMonth && (day < 0 || day >= 31)) { 
+0

經典 - ||不包裹在parens中。 – Jess 2013-03-08 02:49:05

相關問題