我的代码是这样的:
import java.util.Scanner;
public class CalcPyramidVolume {
public static void pyramidVolume (double baseLength, double baseWidth, double pyramidHeight) {
double volume;
volume = baseLength * baseWidth * pyramidHeight * 1/3;
return;
}
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;
}
}
它说打印不能在空类型中完成。我只是不明白为什么...
一个无效的方法不返回任何东西,你可以追加到的主要方法是字符串。您将需要使该方法返回一个double值,然后返回您的变量volume:
public static double pyramidVolume (double baseLength, double baseWidth, double pyramidHeight) {
double volume;
volume = baseLength * baseWidth * pyramidHeight * 1/3;
return volume;
}
或更短:
public static double pyramidVolume (double baseLength, double baseWidth, double pyramidHeight) {
return baseLength * baseWidth * pyramidHeight * 1/3;
}
另请参见:http : //en.wikibooks.org/wiki/Java_Programming/Keywords/void
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句