方法定义 - 金字塔的体积 - 将方法调用返回到另一个方法
Method definition - Volume of a pyramid - Returning method call to another method
我无法弄清楚是什么原因导致我收到 return 我试图调用的 pyramidVolume 的错误。这个问题来自zyBooks,我只能编辑class CalcPyramidVolume。这是我的代码
import java.util.Scanner;
public class CalcPyramidVolume {
double calcPyramidVolume(double baseLength, double baseWidth, double baseHeight);
double baseArea;
double pyramidVolume;
baseArea = baseLength * baseWidth;
pyramidVolume = baseArea * baseHeight * (1.0/3.0);
return pyramidVolume;
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
double userLength;
double userWidth;
double userHeight;
userLength = scnr.nextDouble();
userWidth = scnr.nextDouble();
userHeight = scnr.nextDouble();
System.out.println("Volume: " + pyramidVolume(userLength, userWidth, userHeight));
}
}
所以我试图将输出语句中的参数传递给 calcPyramidVolume 方法的参数。
我从编译器收到以下错误消息。
CalcPyramidVolume.java:8: error: <identifier> expected
baseArea = baseLength * baseWidth;
^
CalcPyramidVolume.java:9: error: <identifier> expected
pyramidVolume = baseArea * baseHeight * (1.0/3.0);
^
CalcPyramidVolume.java:11: error: illegal start of type
return pyramidVolume;
^
CalcPyramidVolume.java:11: error: <identifier> expected
return pyramidVolume;
^
4 errors
我不确定 identifier expected 是什么意思,我已经对变量的拼写和声明进行了三重检查。
终于明白了!我只能更改 main() 方法之上的内容。在更彻底地查看我的文本 material 之后,我发现我需要添加一个 public class 静态双金字塔体积。我只在下面列出了我对更新后的 class 的更正,因为其余的都是一样的。
public class CalcPyramidVolume {
public static double pyramidVolume(double userLength, double userWidth, double userHeight) {
double baseArea; //added public static class with same variables as in the main method
baseArea = userLength * userWidth;
double pyramidVolume = baseArea * userHeight * (1.0/3.0); //added double type to pyramidVolume since it's not declared inside the braces
return pyramidVolume;
}
我无法弄清楚是什么原因导致我收到 return 我试图调用的 pyramidVolume 的错误。这个问题来自zyBooks,我只能编辑class CalcPyramidVolume。这是我的代码
import java.util.Scanner;
public class CalcPyramidVolume {
double calcPyramidVolume(double baseLength, double baseWidth, double baseHeight);
double baseArea;
double pyramidVolume;
baseArea = baseLength * baseWidth;
pyramidVolume = baseArea * baseHeight * (1.0/3.0);
return pyramidVolume;
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
double userLength;
double userWidth;
double userHeight;
userLength = scnr.nextDouble();
userWidth = scnr.nextDouble();
userHeight = scnr.nextDouble();
System.out.println("Volume: " + pyramidVolume(userLength, userWidth, userHeight));
}
}
所以我试图将输出语句中的参数传递给 calcPyramidVolume 方法的参数。
我从编译器收到以下错误消息。
CalcPyramidVolume.java:8: error: <identifier> expected
baseArea = baseLength * baseWidth;
^
CalcPyramidVolume.java:9: error: <identifier> expected
pyramidVolume = baseArea * baseHeight * (1.0/3.0);
^
CalcPyramidVolume.java:11: error: illegal start of type
return pyramidVolume;
^
CalcPyramidVolume.java:11: error: <identifier> expected
return pyramidVolume;
^
4 errors
我不确定 identifier expected 是什么意思,我已经对变量的拼写和声明进行了三重检查。
终于明白了!我只能更改 main() 方法之上的内容。在更彻底地查看我的文本 material 之后,我发现我需要添加一个 public class 静态双金字塔体积。我只在下面列出了我对更新后的 class 的更正,因为其余的都是一样的。
public class CalcPyramidVolume {
public static double pyramidVolume(double userLength, double userWidth, double userHeight) {
double baseArea; //added public static class with same variables as in the main method
baseArea = userLength * userWidth;
double pyramidVolume = baseArea * userHeight * (1.0/3.0); //added double type to pyramidVolume since it's not declared inside the braces
return pyramidVolume;
}