2014-02-19 33 views
-2

簡單地說,我試圖使用一種方法來做一個計算並返回計算。它說明兩個無法弄清楚爲什麼java拋出返回類型錯誤

Invalid method declaration;return type required 
Incompatible types;unexpected return value 

這是類:

package shapes; 

public class RectangleCalc { 
    private double length; 
    private double width; 
} 

public RectangleCalc(double length, double width){ 
    this.length = length; 
    this.width = width; 
} 

public getArea() { 
    return length * width; 
} 

基本方法getArea()是扔我上面列出的錯誤。我知道爲什麼。

+2

getArea()方法的返回類型是什麼? – rgettman

+1

你在「私人雙倍寬度」之後關閉了你的課程;還是它是一個錯字? – injecteer

回答

0

您缺少返回類型。

public double getArea() { 
    return length * width; 
} 
1

你想要得到的計算面積,但你沒有申報的getArea方法的返回類型。它應該是

//double is the return type of the method. 
//Java requires you declared a return type 
public double getArea() 

退房本教程的返回類型http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

+0

謝謝,知道這是愚蠢的。 – Eric

+0

@Eric我不會說這是愚蠢的,有些語言不需要聲明方法或函數的返回類型(例如Javascript)。您可能是Java的新手。事先做一點研究可以解決你的大部分問題:) – Bren

0

1.Need爲函數返回類型。

2.需要在類中包含整個函數和變量。

public class RectangleCalc { 
    private double length; 
    private double width; 


    public RectangleCalc(double length, double width){ 
     this.length = length; 
     this.width = width; 
    } 

    public double getArea() { 
     return length * width; 
    } 
} 
相關問題