2013-08-28 84 views
0

我正在嘗試實現將DD/MM/YY輸入轉換爲DD/MM/YYYY輸入的程序的一部分。我使用了if語句來要求程序檢查輸入的數字是否爲< 13,如果是,則將2000添加到輸入數字 - 如果輸入12,則輸出2012。如果數量> 13 < 100,我已要求計劃增加1900If語句的編譯錯誤

我很新的Java和我遇到,我一直無法克服到目前爲止(從36上下兩個編譯器錯誤,哈哈)。編譯器錯誤是「不是一個聲明」。這兩個引用與else語句的行。代碼如下(顯然還是在製品和未完成的計劃至今) -

import java.util.*; 

public class FindDay4Birthdate 
{ 
    public static void main(String[] args) 
    { 
     String dayInput = ""; 
     String monthInput = ""; 
     String yearInput = ""; 
     int bday; 
     int bmonth; 
     int byear; 

     String daysList[] = { 
      "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 
     }; 
     String monthList[] = { 
      "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" 
     }; 

     Scanner sc = new Scanner(System. in); 
     sc.useDelimiter("[-/.\\s]"); 

     System.out.print("Please enter your date of birth (dd/mm/yyyy) - "); 

     if (sc.hasNext()) 
     { 
      dayInput = sc.next(); 
      monthInput = sc.next(); 
      yearInput = sc.next(); 

      bday = Integer.parseInt(dayInput); 
      bmonth = Integer.parseInt(monthInput); 
      byear = Integer.parseInt(yearInput); 


     } // end if statement 

     if (byear = (byear > 0)); 
     { 
      if (byear = (byear < 13)) 
      { 
       byear = (byear + 2000); 
      } 
      else(byear = (byear > 13 && byear < 100)); 
      { 
       byear = (byear + 1900); 
      } 
     } // end if statement 


     bmonth -= 1; //set month to correct array 

     String day = daysList[bday]; 
     String month = monthList[bmonth]; 

     int yearCount = (byear - 1901); 
     int daysInYear = 365; 
     int dayCount = (daysInYear * yearCount); 

     System.out.println("You were born on " + day + " " + bday + " " + month + " " + byear); 

     System.out.println(yearCount + " " + daysInYear + " " + dayCount); // test output only 

     /* Todo 
     * 
     *TASK 2 - 
     *Implement daysPerMonth - Feb set to 28 days 
     *Use count to determine correct day for birthdate 
     * 
     *TASK 3 - 
     *User input needs to allow char based months and 2 digit years need to be converted to 4 digit years 
     *Implement leap years */ 

    } 
} 
+0

'byear =(byear> 0)'你確定你知道這裏發生了什麼嗎? –

+0

基本上我想說只要輸入年份超過0,就這樣做。考慮一下,如果有人輸入00(2000),這將是一個問題。也許如果我使用=> 0? –

+0

多數民衆贊成在Y2K問題一遍:P。但是回到原點。既然你正在檢查,你不應該分配。基本上通過if。在第一行之前,例如12。下一步它是'true',然後'byear <13'沒有任何意義。 –

回答

4

後刪除分號你的if語句

if (byear=(byear >0)); 同樣,對於以下其他還有

一如果將其視爲區塊的結尾,則以分號結尾。所以,你所定義的其他不被視爲已preceeded它

+0

我只是偷偷摸摸,發現在我讀這本書之前!當它從if(byear =(byear> 0))中移除時它工作正常,但是當我在else語句後刪除分號時,程序返回了一個編譯器錯誤,表示仍然期待分號。 –

1

一旦修復編譯器錯誤的if語句的一部分,你將有邏輯錯誤,從此塊

if (byear=(byear >0));{ 
     if (byear=(byear <13)); 
     { 
      byear=(byear + 2000); 
     } 
     else (byear =(byear >13 && byear <100)); 
     { 
      byear=(byear + 1900); 
     } 
    } // end if statement 

byear=(byear >0)意味着byear現在是byear>0的結果,其可以是truefalse。現在下一個條件不再有意義了。

if中的所有byear = <condition>語句改爲<condition>。即(不復制粘貼,瞭解差異)

if (byear >0){ 
     if (byear <13); 
     { 
      byear=(byear + 2000); 
     } 
     else (byear >13 && byear <100); 
     { 
      byear=(byear + 1900); 
     } 
    } // end if statement 
1

這部分是你的問題。

if (byear = (byear > 0)); 
{ 
    if (byear = (byear < 13)) 
    { 
     byear = (byear + 2000); 
    } 
    else(byear = (byear > 13 && byear < 100)); 
    { 
     byear = (byear + 1900); 
    } 
} // end if statement 

您所做的第一個錯誤是if語句末尾的分號。

if (byear = (byear > 0)); // This ; symbol is to be removed 

這會導致在不執行任何語句的情況下結束if塊。

下一個錯誤是您如何嘗試比較值。這裏。

byear = (byear > 0); 

此語句將與true的byear變量如果byear大於0,否則使其false這導致由於byear是一個整數,而不是一個布爾值編譯錯誤。因此,將您的第一條語句更改爲

if (byear > 0) 

請參閱,不需要將其分配給自己。以相同的方式刪除所有的比較中的任務使你的錯誤部分

if (byear > 0); 
{ 
    if (byear < 13) 
    { 
     byear = (byear + 2000); 
    } 
    else(byear > 13 && byear < 100); 
    { 
     byear = (byear + 1900); 
    } 
} // end if statement 

現在,它應該編譯。