如何修复方法错误
我只是想确保我在这里是正确的。我想方法添加到如何修复方法错误
- 变化高度
- 更改宽度
- 更改坐标
- 计算周长
-
计算面积
public class MyRectangle { public int width; public int height; public int y; public int x; public MyRectangle() { width=10; height=10; y=10; x=10; public int MyRectangle; public MyRectangle(int width, int height, int y, int x, int MyRectangle) { this.width = width; this.height = height; this.y = y; this.x = x; this.MyRectangle = MyRectangle; } }
和我也得到非法开始表达式错误在我的方法。
这是你的问题,你不能在方法中有方法。 但是,这是由于你没有关闭你的方法的括号。 我定你的代码,并添加你想要的方式:
public class MyRectangle {
//Best to group your variables up here
public int MyRectangle;
public int width;
public int height;
public int y;
public int x;
public MyRectangle() {
width = 10;
height = 10;
y = 10;
x = 10;
}//Make sure to close this method with the bracket
public MyRectangle(int width, int height, int y, int x, int MyRectangle) {
this.width = width;
this.height = height;
this.y = y;
this.x = x;
this.MyRectangle = MyRectangle;
}
/**
* Changes the current height to the given new height
* @param newHeight
*/
public final void changeHeight(int newHeight) {
height = newHeight;
}
/**
* Changes the current width to the given new width
* @param newWidth
*/
public final void changeWidth (int newWidth) {
width = newWidth;
}
/**
* Calculates the current perimeter based on the width and height
* @return parameter ofd the rectangle
*/
public final int getPerimeter() {
return ((2 * width) + (2 * height));
}
/**
* Calculates the area based on the width and height
* @return area of the rectangle
*/
public final int getArea() {
return (width * height);
}
public final void changesXCoordinate(int newX){
x = newX;
}
public final void changesYCoordinate(int newY){
y = newY;
}
public final void changesCoordinate(int newX, int newY) {
x = newX;
y = newY;
}
}
我会更加很快解释,只是想后正确的代码第一:目前的情况是P
,它还挺很难理解什么呢你正在寻找。
如果这是您正在寻找的,请将此标记为正确答案:D
我想知道为什么我收到-1,请你解释一下,以便我可以改进我的答案并为将来的参考 – TheMuffinCoder 2015-02-07 02:03:12
不是downvoter ,但也许是因为你在回答,而你不明白他在找什么 – Tarik 2015-02-07 02:07:44
谢谢Tarik!但我只是修正了他所问的错误......如果我把这个作为答案,我很抱歉,我认为这会对他有帮助;(我失去了试图帮助他的代表 – TheMuffinCoder 2015-02-07 02:09:00
问题是什么?以及为什么要设置一个与该类名称相同的变量,请首先更改该名称,这样您的班级可能会出现更多readbale – Tarik 2015-02-07 01:54:18
您在第一个构造函数中忘记了关闭'}'。请检查您的代码是否有语法错误。 – 2015-02-07 01:55:01
您不能在方法中声明'public int MyRectangle;',也不能在方法中声明方法 – TheMuffinCoder 2015-02-07 01:56:40