Java错误无法识别错误

时间:2016-08-14 13:46:08

标签: java

这是Java代码,我创建了4个类3构造函数,我收到错误:

  

类Rect中的方法区域不能应用于给定类型

对于2类的其余部分也存在类似的错误。在这个程序中我基本上创建了4个类,1个用于计算rect的区域,1个用于计算Tri的区域,1个用于计算Square的区域,最后一个用于访问main函数。

我已为所有3个类rect 3和square创建了3个构造函数,我无法在此程序中发现错误。

class Rect    //1st class rect
{
    double l, b;   //variables

    Rect(double l, double b)     //constructor for rect 
    {
        this.l = l;
        this.b = b;
    }

    double area(double l, double b) //method to cal Rect area
    {
        return l * b;
    }
}

class Square   //square class
{
    double s;

    Square(Double s)   //constructor for class
    {
        this.s = s;
    }

    double area(double s) //method to cal area for square
    {
        return s * s;
    }
}

class Tri   // class for triangle 
{
    double l, b, h;   //variables

    Tri(double l, double b, double h)   // constructor for tri
    {
        this.l = l;
        this.h = h;
        this.b = b;
    }

    double area(double l, double b, double h) //method to cal area for tri
    {
        return 0.5 * l * b * h;
    }
}

class Area3 {
    public static void main(String args[]) {
        Rect r = new Rect(10, 10);  //constructor initialization for Rect
        Square s = new Square(15.0);//constructor initialization for Square
        Tri t = new Tri(10.0, 20.0, 30.0);//constructor initialization for Tri
        System.out.print(" " + r.area() + "" + s.area() + "" + t.area()); //print areas
    }
}

3 个答案:

答案 0 :(得分:1)

您的区域方法声明声明区域方法接受参数。有了这些声明,你就不能说

Rect r = new Rect(1,4);
r.area();

只需从区域方法中删除双参数值

即可

答案 1 :(得分:0)

你必须创建没有参数的区域方法,这里是解决方案,

 class Rect // 1st class rect
{
    double l, b; // variables

    Rect(double l, double b) // constructor for rect
    {
        this.l = l;
        this.b = b;
    }

    double area(){
        return this.l * this.b;
    }

    double area(double l, double b) // method to cal Rect area
    {
        return l * b;
    }
}

class Square // square class
{
    double s;

    Square(Double s) // constructor for class
    {
        this.s = s;
    }

    double area(){
        return this.s * this.s;
    }

    double area(double s) // method to cal area for square
    {
        return s * s;
    }
}

class Tri // class for triangle
{
    double l, b, h; // variables

    Tri(double l, double b, double h) // constructor for tri
    {
        this.l = l;
        this.h = h;
        this.b = b;
    }


    double area(){
        return 0.5 * this.l * this.b * this.h;
    }

    double area(double l, double b, double h) // method to cal area for tri
    {
        return 0.5 * l * b * h;
    }
}

class Area3 {
    public static void main(String args[]) {
        Rect r = new Rect(10, 10); // constructor initialization for Rect
        Square s = new Square(15.0);// constructor initialization for Square
        Tri t = new Tri(10.0, 20.0, 30.0);// constructor initialization for Tri
        System.out.print(" " + r.area() + " and " + s.area() + " and " + t.area()); // print
                                                                        // areas
    }
}

希望这有帮助,顺便说一下,它在我的电脑上工作。

答案 2 :(得分:0)

看看你的承包商,他们都收到了争论。 而你所有的地区()也都在争论。

但!!在你的主要部分,你正在调用区域()并且不提供任何值。

只需从area()中删除接收参数即可。