2016-02-26 102 views
1

我正在寫一個程序,我應該有用戶輸入一個從0 - 4000年的日期。我應該看看日期是否有效,如果它是一個飛躍一年或沒有。我在代碼中遇到問題。 我得到了一個沒有錯誤的第57行。 我也不確定如何說如果日期有效或不是如何說。 IE:這個日期是有效的,是一個閏年 - 或無效不是一個閏年...等等...驗證日期從用戶輸入

我還是一個初學者,所以我不想爲我寫的代碼,但我想知道如何解決它!謝謝。

import java.util.*; 

public class LegalDate //file name 
{ 
     public static void main (String [] args) 

    { 
     Scanner kb = new Scanner (System.in); //new scanner 
     //name the variables 
     int month, day, year; 
     int daysinMonth; 
     boolean month1, year1, day1; 
     boolean validDate; 
     boolean leapYear; 



     //ask the user for input 
     //I asked the MM/DD/YYYY in seperate lines to help me visually with the program 
     System.out.println("Please enter the month, day, and year in interger form: "); 
     kb.nextInt(); 

     //now I'm checking to see if the month and years are valid 
     if (month <1 || month >12) 
      { month1 = true;} 
     if (year <0 || year >4000) 
      {year1= true;} 

     //I'm using a switch here instead of an if-else statement, which can also be used 


      switch (month) { 
       case 1: 
       case 3: 
       case 5:    //months with 31 days 
       case 7: 
       case 8: 
       case 10: 
       case 12: 
        numDays = 31; 
        break; 
       case 4: 
       case 6:    //months with 30 days 
       case 9: 
       case 11: 
        numDays = 30; 
        break; 

       case 2: 
        if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) //formula for leapyear 
         numDays = 29; 
          { 
           system.out.println("is a leap year"); 
           } 
        else 
         numDays = 28; 
          { 
           system.out.println("is not a leap year"); 
           } 
        break; 
       default: 
        System.out.println("Invalid month."); 
       break; 

        if (month1 == true) 
        if (day1 == true) 
        if (year1 == true) 
          System.out.println ("date is valid "); 

        else 
        if (month1 == false) 
          System.out.println ("date is invalid"); 

        else 
        if (day1 == false) 
          System.out.println ("date is invalid"); 

         else 
         if (year1 == false) 
          System.out.println ("date is invalid"); 



    }} 

} 

回答

0

您似乎沒有在大括號內的'if'和'else'語句之間放置代碼,這意味着該語句只適用於下一行。例如:

if (a) 
    b = true 
    c = true 
else 
    d = true 

讀作

if (a) { 
    b = true 
} 
c = true 
else { 
    d = true 
} 

希望,你可以看到,編譯器會不明白這一點,作爲一個「其他」語句必須「如果」塊及其相關的後直接發生。

我建議添加一些方法來簡化您的代碼。例如:

public static boolean isLeapYear(int year) { 
    return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)); 
} 

此外,如果您使用布爾變量來存儲信息,您可以在最後整齊地打印它。例如,您可以在代碼頂部將變量「isValid」實例化爲true,如果計算日期無效,則將其設置爲false,並在最後使用if語句打印結果。

我知道你說過你不想讓它爲你寫,但這是證明方法重要性的最簡單方法。希望你能看到它比你的版本更具可讀性嗎?

import java.util.Scanner; 

public class LegalDate { 

    static final int maxYear = 4000; 

    public static void main (String [] args) { 
     int month, day, year; 
     boolean leapYear, validDate = false; 

     Scanner kb = new Scanner (System.in); 
     System.out.println("Please enter the month, day, and year in interger form."); 

     System.out.print("Month: "); 
     month = kb.nextInt(); 
     System.out.print("Day: "); 
     day = kb.nextInt(); 
     System.out.print("Year: "); 
     year = kb.nextInt(); 

     leapYear = isLeapYear(year); 
     validDate = isValidDate(month, day, year); 

     System.out.printf("%nThe date is %svalid and is %sa leap year.%n", validDate ? "" : "not ", leapYear ? "" : "not "); 
     kb.close(); 
    } 

    public static int numDaysInMonth(int month, boolean isLeapYear) { 
     switch (month) { 
     case 1: 
     case 3: 
     case 5: 
     case 7: 
     case 8: 
     case 10: 
     case 12: 
      return 31; 
     case 4: 
     case 6: 
     case 9: 
     case 11: 
      return 30; 
     case 2: 
      if (isLeapYear) { 
       return 29; 
      } else { 
       return 28; 
      } 
     default: 
      return 0; 
     } 
    } 

    public static boolean isLeapYear(int year) { 
     return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)); 
    } 

    public static boolean isValidDate(int month, int day, int year) { 
     return (month >= 1 && month <= 12) && (day >= 1 && day <= numDaysInMonth(month, isLeapYear(year))) && (year >= 0 && year <= maxYear); 
    } 
} 

如果您有任何問題,我會盡我所能來回答他們!

+0

謝謝!我知道我的代碼是草率的,但我不知道它是多麼的分散。感謝您的幫助! – cargoboom

+0

我acuually有一個問題,我們還沒有得到這部分尚未在我的課程'System.out.printf(「%n日期是%s有效,是%閏年%n」,validDate?「」:「不是「,leapYear?」「:」不「)kb.close(); 「我一直在試圖找出它是什麼,但你怎麼用這個?你用它來取代if-else語句嗎? – cargoboom

+1

printf是一種用變量替換佔位符(%s代表字符串,%d代表數字等)的方法,%n是換行符。 'condition'? 'execute_if_true':'execute_if_false'就像你所說的那樣,是if/else的簡寫形式。 因此,如果日期無效,則在第一個%s中插入「not」,如果日期不是閏年,則類似地爲第二個%s(否則插入空字符串)。 kb.close()只是釋放與您的掃描儀相關的資源。這將在您結束程序時自動發生,但最好手動進行。 – Dimpl

0

在第57行,你打開一個新的代碼塊,但沒有任何東西能夠訪問它。我相信你想鍵入:由於布爾比較運算符返回true或false

if (month1 && day1 && year1) 
    System.out.println ("date is valid "); 

else{ 
     numDays = 28; 
     system.out.println("is not a leap year"); 
    } 

作爲一個小技巧,你可以改變這一點:

if (month1 == true) 
       if (day1 == true) 
       if (year1 == true) 
         System.out.println ("date is valid "); 

本,你可以知道條件只是布爾值。由於month1day1year1都是布爾值,所以您不需要將它們與任何內容進行比較。

什麼條件意味着,在你不知道的情況下,如果是和month1day1year1都是真實的,然後打印date is valid

0

你爲什麼不嘗試的Java 8日期時間 API

它驗證日期和做多的越多,你

try { 
     LocalDate date =LocalDate.of(2016, 12, 31); 
     if(date.isLeapYear()) 
      System.out.println("Leap year"); 
     else 
      System.out.println("Not leap year"); 
} 
catch(DateTimeException e) { 
    System.out.println(e.getMessage()); 
} 
0

程序的第50行if-else語句的語法不正確。 這是一個懸而未決的問題。在括號內包含if和else語句的主體應該可以解決這個問題。

if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) 
{ 
    numDays = 29;        
    system.out.println("is a leap year"); 
} 
else 
{ 
    numDays = 28; 
    system.out.println("is not a leap year"); 
} 

您可以利用IDE或注意編譯器錯誤信息來解決此類錯誤。