2016-10-08 164 views
-2

我做的一個類和某些原因「無法找到符號」爲錯誤無法找到符號

perimeter = width + length; 
return perimeter; 

我不知道爲什麼這裏會出現一個錯誤的錯誤(或者是否有別的東西錯了我的代碼,我只是在學校裏開始了Java的,因此任何提示將是有益的。

/** 
* A rectangle has a length and a width. Its perimeter can be calculated. 
*/ 
public class Rectangle 
{ 
private int length; 
private int width; 

/** 
* Constructs a rectangle with a specified length and width 
* @param len the length of the rectangle 
* @param wid the width of the rectangle 
*/ 
public Rectangle(int len, int wid) 
{ 
    length = 0; 
    width = 0; 
} 

/** 
* Sets the length and width of the rectangle 
* @param len the new length 
* @param wid the new width 
*/ 
public void setDimensions(int len, int wid) 
{ 
    length = len; 
    width = wid; 
} 

/** 
* Returns the perimeter of the rectangle 
* @return the perimeter of the rectangle 
*/ 
public int calculatePerimeter() 
{ 
    perimeter = width + length; 
    return perimeter; 
} 
+3

'parameter'從未定義? – Li357

回答

3

perimeter不能在那裏找到,因爲它尚未聲明呢。

要declar e變量,您需要指定它的類型,然後指定它的名稱。

因此,舉例來說,做...

int perimeter = width + length; 
return perimeter; 
+0

這是正確的答案。 –

+0

非常感謝:) – Nimitz