2013-10-22 20 views
0

所以我一直在研究這個程序,並且我已經得到了一些日期來解決問題,但是當我輸入未來一年的二月份時,它表明它的無效當它不應該。 爲了這個節目在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"); 

     } 

    } 

    } 
+0

如果你打算在某些地方做而不是其他人,那麼總是圍繞着你的else/if塊。使它成爲PITA來閱讀。 –

回答

1

你的第一個if語句檢查它是否月份,12月份(這是二月)之間,所以它進入,如果條款,而不是第二個。

移動你的第二個如果單行如果 -s和其他 -s第一

1
  1. ALWAYS使用大括號前。

  2. 讓我們看看你的第一個如果

    // 1. 
    if (inputMonth >= 1 && inputMonth <=12){   
    // 2. 
    if ((inputMonth == 4 || inputMonth == 6 || inputMonth == 9 || inputMonth == 11) && (inputDate >= 1 && inputDate <= 30)) 
    System.out.println("Valid Date"); 
    // 3. 
    else if (inputMonth > currentMonth && inputDate >= 1 && inputDate <=30) 
    System.out.println("Valid Date"); 
    // 4. 
    else if (inputMonth == currentMonth && inputDate > currentDate && inputDate <=30) 
    System.out.println("Valid Date"); 
    // 5. 
    else 
    
    System.out.println("Invalid Date"); 
    } 
    
    • // 1爲真,那麼我們進入。
    • // 2爲false;繼續
    • // 3爲false;繼續
    • // 4爲false;繼續

,現在我們在// 5,打印出 「無效的日期」。

要修理你的邏輯。

相關問題