2017-02-26 60 views
-4

我的代碼沒有返回任何值,我不知道爲什麼。我的任務要求我編寫一個代碼,以mm/dd/yyyy格式接受日期,而im則需要將閏年放入。問題是,我沒有收回任何輸入。我是一個業餘愛好者,我不知道什麼是錯的。我也允許使用案例陳述,但我不知道如何實施案例。爲什麼我的日期驗證碼不起作用?

import java.util.Scanner; 
public class Question1 { 

    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in).useDelimiter("/"); 
     System.out.println("Please enter a date in mm/dd/yyyy format: "); 
     String mm = sc.next(); 
     String dd = sc.next(); 
     String yyyy = sc.next(); 
     int month = Integer.parseInt(mm); 
     int day = Integer.parseInt(dd); 
     int year = Integer.parseInt(yyyy); 

     if (month <= 0 || month>12) 
     { 
      System.out.println("invalid month "); 
     } 
     if (year%4 != 0 || month == 02 || day >= 29) 
     { 
      System.out.println("invalid date"); 
     } 
     if (month == 4 || month == 6 || month == 9 || month == 11 || day >= 31) 
     { 
      System.out.println("Invalid day"); 
     } 
     if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 || day >=32) 
     { 
      System.out.println("Invalid day"); 
     } 
     else 
     { 
      System.out.println("Valid date"); 
     } 
    } 
} 
+0

所以,'year'不能被4整除始終是 「無效的日期」?真?當然,你不是指'&&而不是'||'?在許多檢查中?你知道區別,對吧? – Andreas

+0

修正了它,仍然沒有輸出。現在怎麼了? –

+0

@BamdadGoudarziMoazami你的代碼不返回任何值,因爲該方法是無效的。你想回報什麼?也許你可以發佈整個任務的要求我很難理解你的目標是什麼 – kalenpw

回答

2

該代碼將分隔符設置爲/。然後你輸入類似12/25/2016。第一個sc.next()調用獲得12.第二個獲得25.第三個......等待,因爲它沒有看到另一個/因此它不知道你已經完成。如果用當前代碼輸入12/25/2016/,則至少會提供輸出,即使該輸出不正確。

+0

好吧,試試看吧 –

-1

看來你已經把其他地方放在了錯誤的地方。假設你的第二個條件是正確的,而其他所有的都是錯誤的,那麼你的程序也會在對面顯示它的有效日期。 例如,假設任何日期的日期爲30天,那麼它將滿足第二個條件,它會向您顯示「日期無效」。

You should write if else as follows. 
If{ 
    If{ 
      If{ 
      } 
     } 
}else{ 
} 

所有如果必須在一個嵌套的if和else。你的其他序列是錯誤的。

+0

除了不正確的大寫和不正確的縮進,這是錯誤的。它會轉移問題而不是解決問題。如果第三個「if」失敗會發生什麼?它不會碰到'else'塊。 –

+0

你只需要檢查執行第5條件只有當以上4失敗。這就是全部 – jimmy

0

要使用開關罩然後通過下面的代碼:

import java.util.Scanner; 
public class Question1 { 

    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in).useDelimiter("/"); 

     System.out.println("Please enter a date in mm/dd/yyyy/ format: "); 
     String mm = sc.next(); 
     String dd = sc.next(); 
     String yyyy = sc.next(); 
     int month = Integer.parseInt(mm); 
     int day = Integer.parseInt(dd); 
     int year = Integer.parseInt(yyyy); 
     boolean valid = isValidDate(day,month,year); 
     if (valid == true) 
     { 
      System.out.println("Date is Valid"); 
     } 
     else 
     { 
      System.out.println("Date is InValid"); 
     } 
    } 
    public static boolean isValidDate(int day, int month ,int year) 
    { 
     boolean monthOk = (month >= 1) && (month <= 12); 
     boolean dayOk = (day >= 1) && (day <= daysInMonth(year, month)); 

     return (monthOk && dayOk); 
    } 
    private static int daysInMonth(int year, int month) { 
     int daysInMonth; 

    switch (month) { 
    case 1: 
    case 3: 
    case 5: 
    case 7: 
    case 8: 
    case 10: // go through 
    case 12: 
     daysInMonth = 31; 
     break; 
    case 2: 
     if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { 
      daysInMonth = 29; 
     } else { 
      daysInMonth = 28; 
     } 
     break; 
    default: 
     // returns 30 even for nonexistant months 
     daysInMonth = 30; 
    } 
    return daysInMonth; 
    }  

} 

需要輸入作爲12月25日/到2016年/,不是這個2016年12月25日

+0

這個伴侶。這幫助了很多。 –

0

這裏是什麼讓你開始:

final String DELIMITER = "/"; 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Please enter a date in mm/dd/yyyy format:\n "); 

    String date = sc.next(); 
    sc.close(); 

    String[] dateParts = date.split(DELIMITER); 
    //check : if dateParts size is not 3 ....something is wrong 

    String mm = dateParts[0]; 
    String dd = dateParts[1]; 
    String yyyy = dateParts[2]; 

    System.out.println(mm+" "+ dd +" "+ yyyy); 
相關問題