java 方法:金字塔体积
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36255557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Methods: Pyramid Volume
提问by java2019
This is my task that I have to do:
这是我必须完成的任务:
Define a method pyramidVolume with double parameters baseLength, baseWidth, and pyramidHeight, that returns as a double the volume of a pyramid with a rectangular base.
定义一个带有双参数 baseLength、baseWidth 和 pyramidHeight 的方法 pyramidVolume,该方法返回具有矩形底部的金字塔体积的两倍。
Here is my code:
这是我的代码:
import java.util.Scanner;
public class CalcPyramidVolume {
public static void pyramidVolume (double baseLength, double baseWidth, double pyramidHeight) {
baseLength = 1.0;
baseWidth = 1.0;
pyramidHeight = 1.0;
double pyramidVolume = ((baseLength * baseWidth) * pyramidHeight) / 3;
}
public static void main (String [] args) {
System.out.println("Volume for 1.0, 1.0, 1.0 is: " + pyramidVolume(1.0, 1.0, 1.0));
return;
}
}
I can edit only the section of code where I created the pyramidVolume method call. I am getting an error that says 'void' type not allowed here and it is pointing the to system.out line which i can not edit. I am very confused on why it is giving me an error on that line.
我只能编辑我创建了 pyramidVolume 方法调用的代码部分。我收到一个错误,指出此处不允许使用“void”类型,它指向我无法编辑的 system.out 行。我很困惑为什么它会在那条线上给我一个错误。
回答by Sanj
pyramidVolume
return type is void
. Change return type to double
as below:
pyramidVolume
返回类型是void
.将返回类型更改double
为如下:
public static double pyramidVolume (double baseLength, double baseWidth, double pyramidHeight) {
double pyramidVolume = ((baseLength * baseWidth) * pyramidHeight) / 3;
return pyramidVolume;
}