如何在 Java 编程 (CodeHS) 中找到金字塔的表面积?

How to find the surface area of a pyramid in Java programming (CodeHS)?

我在 CodeHS 上有一个作业,要为金字塔的表面积计算器编程,它会打印出错误的表面积,小数点后几位。我不明白这是怎么不正确的(下面的代码)。

我已经尝试插入 Google 的表面积公式,但它没有用,打印出错误的数字。

 public double surfaceArea() {
  double hw = (double)width/2;
  double hl = (double)length/2;
  double slantHeight1 = ((double)Math.sqrt( (double)height*height + 
   (double)hw*hw ));
  double slantHeight2 = ((double)Math.sqrt( (double)height*height + (double)hl*hl ));

  return (double)(((double)0.5 * 2 * slantHeight1 * width)
  + ((double)0.5 * 2 * slantHeight2 * length) 
  + (length * width));

示例:对于长度为 1、宽度为 3、高度为 5 的金字塔,它应该打印 23.29,但它打印了 23.69,我不知道为什么?

另一种解决方案:这是直立四棱锥表面积的方程式:

这可以简单地写成:

    public static void main(String[] args) {

        double length = 1;
        double width = 3;
        double height = 5;
        double resultPyramidArea = (length * width) + (length * Math.sqrt(Math.pow(width / 2, 2) +
                Math.pow(height, 2))) + (width * Math.sqrt(Math.pow(length / 2, 2) + Math.pow(height, 2)));

        System.out.println(resultPyramidArea);
    }

改变这个:

  return (double)(((double)0.5 * 2 * slantHeight1 * width)
  + ((double)0.5 * 2 * slantHeight2 * length) 
  + (length * width));

对此:

  return (double)(((double)0.5 * 2 * slantHeight1 * length)
  + ((double)0.5 * 2 * slantHeight2 * width) 
  + (length * width));

你把公式弄错了