我只是想确保我在这里是正确的。我正在尝试添加方法
计算面积
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;
}
}
我的方法也出现了非法的表达式错误启动。
答案 0 :(得分:1)
这是你的问题,你不能在方法中使用方法。 但这是因为你没有为你的方法关闭括号。 我修改了你的代码并添加了你想要的方法:
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