2014-12-01 78 views
-4

我不能讓這些編譯它似乎是我試圖傳遞布爾值的地方。第一個有2個錯誤,沒有任何意義,我Java傳遞布爾值

public class Date { 
    public int m; 
    public int d; 
    public int y; 
    boolean isLeapYear; 

    public String monthIs(){ 
     return month; 
     m = Integer.parseInt(month); 
    } 

    public String dayIs(){ 
     return day; 
     d = Integer.parseInt(day); 
    } 

    public Date(String year){ 
     y = Integer.parseInt(year); 
     // Is y Divisible by 4 
     if (y % 4 == 0){ 

      // Is y Divisible by 4 but not 100 
      if (y % 100 != 0) 
       isLeapYear = true; 

      // Is y Divisible by 4 and 100 and 400 
      else if (y % 400 == 0) 
       isLeapYear = true; 

      // It's Divisible by 4 and 100 but not 400 
      else 
       isLeapYear = false; 
     } 
     // It's not divisible by 4 
     else 
     { 
      isLeapYear = false; 
      public boolean getisLeapYear() 
      { 
       return isLeapYear; 
      } 
     } 
    } 
} 

DateJDialog類:

import javax.swing.JOptionPane; 
/** This program runs the Date class to determine if 
* the date entered falls within a leap year. 
*/ 
public class DateJDialog 
{ 
    public static void main(String[] args) 
    { 
     String month; 
     String day; 
     String year; 
     boolean isitLeapYear; 
     Date date; 
     //Get Input 
     JOptionPane.showMessageDialog(null, "This program determines if the date 
              entered falls within a leap year."); 
     month = JOptionPane.showInputDialog("What month?"); 
     day = JOptionPane.showInputDialog("What day?"); 
     year = JOptionPane.showInputDialog("What year?"); 

     //Create Date object 
     date = new Date(year); 

     if (date.getisLeapYear()==true); 
     if (isLeapYear = true) 
      JOptionPane.showMessageDialog(null, month + "-" + day + "-" + year 
      + " does fall within a leap year."); 
     else 
      JOptionPane.showMessageDialog(null, month + "-" + day + "-" + year 
      + " does not fall within a leap year."); 
     System.exit(0); 
    } 
} 
+9

第1步:將您的代碼格式化爲人類可讀的。第2步:查看錯誤消息。發生錯誤時不要放棄。 *閱讀錯誤*嘗試解決它。 – David 2014-12-01 20:11:36

+2

你不能在另一種方法的中間定義一個方法('getIsLeapYear')。 – ajb 2014-12-01 20:11:46

+0

你需要做'isLeapYear == true'而不是'isLeapYear = true'-另外,總是發佈你得到的錯誤 - 不要讓我們猜測你在屏幕上看到的是什麼。 – nos 2014-12-01 20:11:58

回答

1

您在代碼中有一些錯誤,我會盡我所能來點他們出去了。

首先,你有這樣的if-statement

if (date.getisLeapYear()==true); 

if-statement必須由開括號括號和閉本體(我相信這將編譯,但無濟於事)。相反:

if (date.getisLeapYear()==true) { 
    //do something 
} 

因爲這似乎是一個boolean吸氣,有條件的檢查可以縮短爲:

if (date.getisLeapYear()) {//this checks for a "true" value, !date.getisLeapYear() checks for false 
    //do something 
} 

您的isLeapYear是真實支票是不正確的,且長度再次輸入,出來的時候不必要。

if (isLeapYear = true) 

應該是:

if (isLeapYear)//again checking if true. !isLeapYear would be checking for false. 

你的構造是建立怪異和完全錯誤的。首先,如果您使用的是if-else聲明,則其必須使用大括號

下是有效的,但被認爲是不好的做法:

if (condition) 
    //do soemthing 

下需要括號:

if (condition) { 

} else if (another condition) { 

} (...) 

最後,你聲明構造函數中的一個getter。方法只能在類作用域創建,這意味着方法不能在方法內創建。爲了解決這個問題:也

public Date() { 
    (...) 
} 

public boolean getisLeapYear() { 
    return isLeapYear; 
} 

,只是作爲一個提示,你可以在一個聲明的變量鏈的多個實例:

String month; 
String day; 
String year; 

可以寫爲:

String month, day, year; 

我不知道這是否涵蓋了所有錯誤,但這是一個健康的開始。