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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:09:33  来源:igfitidea点击:

Methods: Pyramid Volume

java

提问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

pyramidVolumereturn type is void. Change return type to doubleas below:

pyramidVolume返回类型是void.将返回类型更改double为如下:

public static double pyramidVolume (double baseLength, double baseWidth, double pyramidHeight) {

  double pyramidVolume = ((baseLength * baseWidth) * pyramidHeight) / 3;
  return pyramidVolume;
}