繁体   English   中英

协助我查找错误:方法声明无效; 需要返回类型

[英]Assist me in finding the error: invalid method declaration; return type required

这是我的Java类的最后一个作业,我一直试图通过编译器运行它,但是我不明白代码有什么问题。

在阅读了有关如何解决返回类型问题的信息后,我尝试使用了void,但这使情况变得更糟,也许我将void放在了错误的位置。

public class Exercise09_01 {
    private double width = 1;
    private double height = 1;

    public Rectangle() {
    }

    public Rectangle(double newWidth, double newHeight) {
        width = newWidth;
        height = newHeight;
    }

    public double getArea() {
        return width * height;
    }

    public double getPerimeter() {
        return 2 * (width + height);
    }

    public static void main(String[] args) {
        Rectangle rectangle1 = new Rectangle(4, 40);
        System.out.println("The area of a 4.0 x 40.0 Rectangle is " + 
        rectangle1.getArea());
        System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + 
        rectangle1.getPerimeter());
        Rectangle rectangle2 = new Rectangle(3.5, 35.9);
        System.out.println("The area of a 3.5 x 35.9 Rectangle is " + 
        rectangle2.getArea());
        System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + 
        rectangle2.getPerimeter());
    }
}

这是我上这堂课的最后一个作业,我只希望对此给予任何帮助,不胜感激。

代码中的问题是类名称和构造函数名称不同。

您有两种选择,一种是将构造函数重命名为Exercise01_01或将Rectangle的返回类型视为void。

public class Exercise01_01 {

    private double width = 1;
    private double height = 1;

    public Exercise01_01() {
    }

    public Exercise01_01(double newWidth, double newHeight) {
        width = newWidth;
        height = newHeight;
    }

    public double getArea() {
        return width * height;
    }

    public double getPerimeter() {
        return 2 * (width + height);
    }

    public static void main(String[] args) {
        Exercise01_01 rectangle1 = new Exercise01_01(4, 40);
        System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
        System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + rectangle1.getPerimeter());
        Exercise01_01 rectangle2 = new Exercise01_01(3.5, 35.9);
        System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
        System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + rectangle2.getPerimeter());
    }

}
public Rectangle() {
}

就Java而言,这是一种方法。 并且所有方法都必须具有返回类型。

public Rectangle(double newWidth, double newHeight) {
width = newWidth;
  height = newHeight;
  }

和这里一样。

除非确实需要在没有设置这些值的情况下制作第一个,否则您实际上不需要第一个。

您可以重命名它们,但是您可能只想重命名Rectangle

构造函数名称应与类名称相同

    public class Exercise09_01 {
        private double width = 1;
        private double height = 1;

        public Exercise09_01() {
        }

        public Exercise09_01(double newWidth, double newHeight) {
            width = newWidth;
            height = newHeight;
        }
   }
public class Exercise09_01 {
private double width = 1;
private double height = 1;

public Exercise09_01() {
}

public Exercise09_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}

public double getArea() {
return width * height;
}

public double getPerimeter() {
return 2 * (width + height);
}

public static void main(String[] args) {
Exercise09_01 rectangle1 = new Exercise09_01(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + 
rectangle1.getPerimeter());
Exercise09_01 rectangle2 = new Exercise09_01(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + 
rectangle2.getPerimeter());
}
}

感谢您提供的所有帮助,这是通过编译器传递的代码。 只是留在这里供以后的访客。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM