我应该怎么解决这个问题

时间:2014-06-21 06:21:24

标签: java

好的家伙我坚持这个得到10个错误,有人能帮帮我吗?这些错误询问标识符和内容。我现在坚持了很长一段时间,只是不知道我哪里出错了?我没有设置正确的变量或什么?这个错误:

DemoSquare.java:36:错误:预期    公共广场(高度,宽度)                        ^

DemoSquare.java:36:错误:预期    公共广场(高度,宽度)                               ^

DemoSquare.java:36:错误:';'预期    公共广场(高度,宽度)                                ^

DemoSquare.java:39:错误:预期        square_width = width;                    ^

DemoSquare.java:42:错误:预期的类,接口或枚举    public int getheight()           ^

DemoSquare.java:47:错误:预期的类,接口或枚举    }    ^

DemoSquare.java:48:错误:预期的类,接口或枚举    public int getwidth(){           ^

DemoSquare.java:50:错误:期望的类,接口或枚举    }    ^

DemoSquare.java:51:错误:预期的类,接口或枚举    public int computeSurfaceArea()           ^

DemoSquare.java:54:错误:期望的类,接口或枚举          surfaceArea =(getheight()* getwidth());          ^

DemoSquare.java:55:错误:预期的类,接口或枚举          返回surfaceArea;          ^

DemoSquare.java:57:错误:预期的类,接口或枚举    }    ^ 12个错误

工具已完成,退出代码为1

`import java.util.Scanner;
public class DemoSquare
{
public static void main(String args[])
{
 Scanner input = new Scanner(System.in);

   //Prints asking for Square user input
    System.out.println("Please enter square height: ");
    int square_height = input.nextInt();
    System.out.println("Please enter square width: ");
    int square_width = input.nextInt();

   //Prints asking for Cube user input
    System.out.println("Please enter cube height: ");
    int cube_height = input.nextInt();
    System.out.println("Please enter cube width: ");
    int cube_width = input.nextInt();
    System.out.println("Please enter cube depth: ");
    int cube_depth = input.nextInt();

    //Prints for Square Area :)
    Square aSquare = new Square(square_height, square_width);
    System.out.println("Square Area is: "+ square.computeSurfaceArea());
    Cube aCube = new Cube(cube_height, cube_width, cube_depth);
    System.out.println("Cube Area is: " + cube.computeSurfaceArea());

}

}

public class Square
 {
  int square_height = 0;
  int square_width = 0;

  public Square(height, width) (
   square_height= height;
   square_width= width;
  }

  public int getheight()
 {

    return square_height;

  }

    public int getwidth(){

     return square_width;
  }

     public int computeSurfaceArea()
  {
  int surfaceArea = square_height * square_width;
     surfaceArea = (getheight() * getwidth());
     return surfaceArea;

   }

   }
     public class Cube extends Square
  {

       int cube_height = 0;
       int cube_width = 0;
       int cube_depth = 0;


        public Cube(int height, int width, int depth) {
        super(cube_height, cube_width);
        cube_depth = depth;
   }

    @Override
     public int getcube_width() {
      return cube_width;
  }

  @Override
     public int getcube_height() {
      return cube_Height;
  }

  public int getcube_depth() {
      return cube_depth;
  }

  @Override
  public int computeSurfaceArea() {

      int cube_surface_area = (cube_height * cube_width * cube_depth);

      return cube_surface_area;
  }
  }`

1 个答案:

答案 0 :(得分:0)

我注意到的第一件事是,

public Square(height, width)

应该是

public Square(int height, int width)

接下来我注意到的是

public int computeSurfaceArea()
{
  // int surfaceArea = square_height * square_width;
  // surfaceArea = (getheight() * getwidth());
  return getheight() * getwidth();
}

最后,我建议您遵循标准的Java命名实践; getHeight()getWidth(),然后在getHeight()getWidth()getDepth()Cube。也称它为getSurfaceArea()可能更好。一致性很好(并记住maintenance programmer)。

请注意,这也适用于您的Cube构造函数,以便快速修复 -

public Cube(int height, int width, int depth) {
    super(height, width); // cube_height, cube_width are defaulting to 0.
    cube_depth = depth;
    cube_height = height; // <-- you called it cube_height, not just height.
    cube_width = width;
}