簡體   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