2014-12-01 75 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; 

我不知道这是否涵盖了所有错误,但这是一个健康的开始。