需要帮助理解,java:应在下面的代码中

需要帮助理解,java:应在下面的代码中,java,Java,下面是一个查找矩形区域的简单代码。 它运行在编译错误,不确定是什么错误 请帮忙 我得到了这个错误-java:expected:16 public class test { public static void main(String[] args) { float l = 7.33f; float b = 4.22f; Rectangle R1 = new Rectangle( l,b); float area = R1.area(); System.

下面是一个查找矩形区域的简单代码。 它运行在编译错误,不确定是什么错误 请帮忙

我得到了这个错误-java:expected:16

public class test {
  public static void main(String[] args) {
    float l = 7.33f;
    float b = 4.22f;
    Rectangle R1 = new Rectangle( l,b);
    float area = R1.area();
    System.out.println("The area of the Rectangle R1 is " + area);
  }
}

矩形类中的构造函数编写不正确。应该是这样的-

 class Rectangle {
      float l;
      float b;
      
     public Rectangle(float l, float b){ // need to declare the variable types 
        this.l= l;
        this.b= b;
      }

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

这是因为你的构造函数,public void rectangle,b,你把它变成了一个方法 其次,您缺少两个浮动,因此:


构造函数签名应该是public Rectanglefloat l,float b{。构造函数没有返回类型,甚至没有void。但它确实有为其每个参数指定的类型。
 class Rectangle {
      float l;
      float b;
      
     public Rectangle(float l, float b){ // need to declare the variable types 
        this.l= l;
        this.b= b;
      }

      public float area(){
        return this.l*this.b;
      }
    }
    public static void main(String[] args) {
        float l = 7.33f;
        float b = 4.22f;
        Rectangle R1 = new Rectangle(l,b);
        float area = R1.area();
        System.out.println("The area of the Rectangle R1 is " + area);
    }
}
 class Rectangle {
    float l;
    float b;
    public Rectangle(float l, float b){//changed here
        this.l= l;
        this.b= b;
    }
    public float area(){
        return this.l*this.b;
    }