0

我遇到问题的第一段代码是 DemoSquare - 当我运行它时它会崩溃。错误是:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
  symbol:   variable area
  location: class Square
    at Square.computeSurfaceArea(DemoSquare.java:57)
    at DemoSquare.main(DemoSquare.java:23)
Java Result: 1

我的代码是 -

// package demosquare;
import java.util.*;

public class DemoSquare {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the height of the square/rectangle");
        double sqHeight = input.nextDouble();
        System.out.println("Please enter the width of the square/rectangle");
        double sqWidth = input.nextDouble();

        System.out.println("Please enter the height of the Cube/Rectangular Prism");
        double cuHeight = input.nextDouble();
        System.out.println("Please enter the width of the Cube/Rectangular Prism");
        double cuWidth = input.nextDouble();
        System.out.println("Please enter the depth of the Cube/Rectangular Prism");
        double cuDepth = input.nextDouble();

        // Create a square and print out the information about it
        Square square = new Square(sqHeight, sqWidth);
        System.out.println("A Square with dimensions " + square.getHeight()
                + " by " + square.getWidth() + " has a surface area of "
                + square.computeSurfaceArea());
        // Create a cube and print out the information about it.
        Cube cube = new Cube(cuHeight, cuWidth, cuDepth);
        System.out.println("A Cube with dimensions " + cube.getHeight()
                + " by " + cube.getWidth() + " by " + cube.getDepth() + " has a surface area of "
                + cube.computeSurfaceArea());

    } //  end main method
}  //end class DemoSquare

class Square {
    // enter the code to create the square class here

    double sqHeight = 0;
    double sqWidth = 0;

    public Square(double height, double width) {
        sqHeight = height;
        sqWidth = width;
    }

    public double getWidth() {
        return sqWidth;
    }

    public double getHeight() {
        return sqHeight;
    }

    public double computeSurfaceArea() {
        double surfaceArea = sqHeight * sqWidth;
        surfaceArea = (getHeight() * getWidth());
        return area;
    }
}

class Cube extends Square {

    double sqDepth = 0.00;
    // enter the cube class code here

    public Cube(double height, double width, double depth) {
        super(height, width);
        sqDepth = depth;
    }

    @Override
    public double getWidth() {
        return sqWidth;
    }

    @Override
    public double getHeight() {
        return sqHeight;
    }

    public double getDepth() {
        return sqDepth;
    }

    @Override
    public double computeSurfaceArea() {
        //Surface Area = 2hw + 2wd + 2dh
        double tsa = (2 * sqHeight * sqWidth) + (2 * sqWidth * sqDepth) + (2 * sqDepth * sqHeight);

        return tsa;
    }
}

我在这段代码中做错了什么?

4

3 回答 3

1

You can tell by the stacktrace where the error lies. Let's review it.

  1. The reason why the program didn't work:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code

    This means your code is currently broken and will not execute by any means until at least compiles.

    Uncompilable source code - cannot find symbol

    symbol: variable area

    This means there's an unknown variable with name area that's been used in your code but is never declared.

  2. Then it comes the location of the error:

    location: class Square

    at Square.computeSurfaceArea(DemoSquare.java:57)

    Now we have where to look. Let's go to the Square class, inside the method computeSurfaceArea, more specifically, at line 57 in the file DemoSquare.java.

    //54. public double computeSurfaceArea() {
    //55.     double surfaceArea = sqHeight * sqWidth;
    //56.     surfaceArea = (getHeight() * getWidth());
    //57.     return area;
    //58. }
    

    Now we found the culprit: return area;. Note that area here is an undeclared variable. As noted in other answers, you probably meant to use surfaceArea instead.

    //54. public double computeSurfaceArea() {
    //55.     double surfaceArea = sqHeight * sqWidth;
    //56.     surfaceArea = (getHeight() * getWidth());
    //57.     return surfaceArea;
    //58. }
    

Do this change and the code will be fixed until now.

Do the same process to fix other problems in your code.

Do a similar process when you encounter other exceptions at runtime like NullPointerException.

于 2013-09-29T18:32:09.800 回答
1

这是您做错的事情:

public double computeSurfaceArea() {
    double surfaceArea = sqHeight * sqWidth;
    surfaceArea = (getHeight() * getWidth());
    return area; // no such variable - it's called surfaceArea...
}

该变量称为表面积,而不是面积。即,您应该这样做:

public double computeSurfaceArea() {
    double surfaceArea = sqHeight * sqWidth;
    surfaceArea = (getHeight() * getWidth());
    return surfaceArea;
}
于 2013-09-29T18:20:37.667 回答
1
double surfaceArea = sqHeight * sqWidth;
    surfaceArea = (getHeight() * getWidth());
    return area;

我想你的意思是

    return surfaceArea ;
于 2013-09-29T18:20:48.387 回答