【问题标题】:How to fix method error如何修复方法错误
【发布时间】:2015-02-07 01:48:34
【问题描述】:

我只是想确保我在这里是正确的。我正在尝试将方法添加到

  • 改变高度
  • 改变宽度
  • 更改坐标
  • 计算周长
  • 计算面积

    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 int MyRectangle;,也不能在方法中声明方法
  • 这里的任何人都可以很容易地给你一个带有改变宽度、高度的方法的代码......但是你必须付出一些努力,你甚至没有尝试编译你的代码

标签: java class methods rectangles


【解决方案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

【讨论】:

  • 我想知道为什么我收到 -1,请您解释一下,以便我可以改进我的答案并供将来参考
  • 不是反对者,但可能是因为您在回答时不明白他在寻找什么
  • 谢谢塔里克!但我只是修复了他所问的错误......很抱歉,如果我把这个作为答案,我认为这会对他有所帮助;(我失去了试图帮助他的代表
  • 别担心,这不算多代表 :) 您可以通过正确编辑某人的问题轻松获得 +2 代表 ;)
  • 我想我几乎涵盖了他会问的任何其他问题,添加了改变高度、宽度、面积和周长的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2021-09-13
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多