Leap不能应用于使用Java编译测试

时间:2013-06-06 12:59:53

标签: java

我已经研究过解决方案并且已经开始研究这个时代了,除了我在编译过程中不断得到这两个错误,特别是变量可能没有被初始化?我在两个类之间交替使用解决方案,但它是类似的错误。

我的问题是:我如何解决这些错误,如下[谢谢]。调用.setIsLeapYear()但没有参数,而在Leap类中使用一个参数定义它。

App.java:129: setisLeapYear(int) in Leap cannot be applied to ()
        aLeap.setisLeapYear(); //aLeap.setisLeapYear(anLeapYear);
             ^
App.java:130: setisNotLeapYear(int) in Leap cannot be applied to ()
        aLeap.setisNotLeapYear(); //aLeap.setisNotLeapYear(anNotLeapYear);

代码中的这一行阻止我成功编译。我已经标出了两行 [第129行] [第130行] ,如下所示。

    **[line 129]** aLeap.setisLeapYear(); //aLeap.setisLeapYear(anLeapYear);
    **[line 130]** aLeap.setisNotLeapYear(); //aLeap.setisNotLeapYear(anNotLeapYear);
    aLeap.sortLeapyear();

    //year=aYear.sortLeapyear();

    oBox.show();
    oBox.print("The " + anLeapYear + " is a leap year."); //leap.anLeapYear //aLeap.anLeapYear //aLeap.isLeapYear
    //"The " + year + " is a leap year.")
    //"The " + year + " is a leap year: " + isLeapYear);
    //System.out.println("This is a leap year!" + isLeapYear);

    oBox.print("The " + anNotLeapYear + " is not a leap year."); 

我还包括两个完整的类,为您提供方便的App类和Leap类。

类App 已编辑。

import javabook.*;

class App
{

public static void main(String args[])
{
    App thisProgram = new App();   



}   
    //outside a main class
    public App()
    {

        //contsructor
        //set variables
        //int aYear = 2000; //year
        int aYear = 2000; EDITED.
        int anLeapYear;
        int anNotLeapYear;

        //declare objects
        MainWindow mWindow;
        Leap aLeap;
        InputBox iBox;
        OutputBox oBox;

        //create objects                
        mWindow = new MainWindow();     
        aLeap = new Leap();
        iBox = new InputBox(mWindow);   
        oBox = new OutputBox(mWindow);  

        mWindow.show();

        //get input of base and height
        aYear = iBox.getInteger("Enter a year: "); //aYear

        int year = aLeap.getisLeapYear();//

        boolean value = true; 
        if (value == true)


        aLeap.setisLeapYear(); //aLeap.setisLeapYear(anLeapYear);
        aLeap.setisNotLeapYear(); //aLeap.setisNotLeapYear(anNotLeapYear);
        aLeap.sortLeapyear();

        //year=aYear.sortLeapyear();

        oBox.show();
        oBox.print("The " + anLeapYear + " is a leap year."); 
        oBox.print("The " + anNotLeapYear + " is not a leap year."); 
        //the end.
    }
}.

类跳跃

class Leap
{
    //public static void main(String args[])

    //data
    //private constants
    final int year; 

    //private variables
    private int isLeapYear;
    private int isNotLeapYear;

    //constructors //Leapyear
    public Leap()
    {
        //this.isLeapYear = 0;      
        //this.isNotLeapYear = 0;   
    }
    //methods - behavious
    public void sortLeapyear() 
    {   
        boolean value = true; 
        if (value == true) 

        if (year % 4 == 0) //((year % 4) == 0) 
        {
        if (year % 100 == 0) 
            {
            if (year % 400 != 0)
            {
            }
            if (year % 400 == 0)
            {
            }
    }
        if (year % 100!= 0)
            {
            }
        }
        if (year % 4 != 0)
            {
            }

        //this.isLeapYear = ( ((year % 400) == 0) || ((year % 4) == 0 && (year % 100) != 0)  ); SHOULD BE IN IF STATEMENT ON LEAP YEAR.
        //this.isLeapYear = ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0));
        //(year % 400 == 0) && (year % 100 != 0) || (year % 4 == 0); //http://ubuntuforums.org/archive/index.php/t-1264964.html
        //http://en.wikiversity.org/wiki/Introduction_to_Programming_in_Java/Boolean_variables
        //this.isLeapYear = ((year % 4) == 0); //(year % 400 == 0);
        //this.isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);  //found   : boolean   //1 + (int) (Math.random() * NUMBER_OF_SIDES);
        //this.isLeapYear = ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0));//1 + (int) (Math.random() * NUMBER_OF_SIDES);
}

        //Set the height and get the height
        public void setisLeapYear(int anLeapYear)
        {
            this.isLeapYear = year;
        }
        //method - Get (accessors) and sets (mutators)
        public int getisLeapYear()
        {
            return(this.isLeapYear);
        }

        public void setisNotLeapYear(int isNotLeapYear)
        {
            this.isNotLeapYear = year;
        }
        //method - Get (accessors) and sets (mutators)
        public int getisNotLeapYear()
        {
            return(this.isNotLeapYear);
        }
}

1 个答案:

答案 0 :(得分:1)

您的问题是,setisLeapYear方法取one argument,当您调用它时,您不会向此方法发送任何内容

解决方案是:
您可以创建不带任何内容的setisLeapYear,例如:

 public void setisLeapYear(////nothing here)
 {
...

 }


int发送到您称之为anLeapYear的方法,就像那样:

aLeap.setisLeapYear(///put number here );