2016-10-08 163 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