gpt4 book ai didi

Java - 解决 DemoSquare 问题

转载 作者:行者123 更新时间:2023-12-01 23:32:52 26 4
gpt4 key购买 nike

我遇到问题的第一段代码是 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;
}
}

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

最佳答案

您可以通过堆栈跟踪来判断错误所在。让我们回顾一下。

  1. 程序无法运行的原因:

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

    这意味着您的代码当前已损坏,并且至少在编译之前不会以任何方式执行。

    Uncompilable source code - cannot find symbol

    symbol: variable area

    这意味着有一个名为 area 的未知变量已在您的代码中使用但从未声明。

  2. 然后是错误的位置:

    location: class Square

    at Square.computeSurfaceArea(DemoSquare.java:57)

    现在我们可以去哪里寻找了。让我们转到 Square 类的 computeSurfaceArea 方法中,更具体地说,位于文件 DemoSquare.java 的第 57 行。

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

    现在我们找到了罪魁祸首:return area;。注意这里的area是一个未声明的变量。正如其他答案中所述,您可能打算使用 surfaceArea 代替。

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

进行此更改,代码将被修复到现在。

执行相同的过程来修复代码中的其他问题。

当您在运行时遇到其他异常(例如 NullPointerException)时,请执行类似的过程。

关于Java - 解决 DemoSquare 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19081582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com