[英]Getting the correct print output from return methods
我正在尝试编写一小段代码,在给定r和h时输出三个数字的最小值,三个数字的最大值以及圆柱体的表面积。
我已经编写了代码,对其进行了编译,并且它完全按照我想要的方式工作。 但是,对于实际的打印输出,我希望它会更好一些。 我对此有麻烦。
在查看下面的代码时,当前输出为:3 56.8半径为3.0且高度为4.5的圆柱的表面积为141.3716694115407
我想在采用圆柱方法之后对实际的打印输出进行建模(即“ 5、7和3的最小值为3”和“ ......的最大值”)。 圆柱部分很容易,因为我的打印语句在方法中...方法本身不是返回类型。
但是作为返回类型的findMin和findMax方法,有人可以向我提供建议或提示,以了解如何使代码实际输出的不仅仅是最小值或最大值吗? 我尝试在main方法下实际在实际的println语句中播放,但始终会出错。
非常感谢...初学者
public class Test {
public static void main(String[] args) {
System.out.println(findMin(5, 7, 3));
System.out.println(findMax(-5.1, 32.5, 56.8));
cylinderSurfaceArea(3.0, 4.5);
}
public static int findMin(int a, int b, int c) {
int minimum = Math.min(a, b);
int minimum2 = Math.min(minimum, c);
return minimum2;
}
public static double findMax(double a, double b, double c) {
double maximum = Math.max(a, b);
double maximum2 = Math.max(maximum, c);
return maximum2;
}
public static void cylinderSurfaceArea(double a, double b) {
double answer = 2 * Math.PI * (a * a) + 2 * Math.PI * a * b;
System.out.println("The surface area of a cylinder with radius " + a + " and height " + b + " is " + answer);
}
}
一种方法是将print
语句放入方法中(我不建议这样做):
public static int findMin(int a, int b, int c)
{
int minimum = Math.min(a, b);
int minimum2 = Math.min(minimum, c);
System.out.println("The minimum of " + a + ", " + b + " and " + c + " is " + minimum2);
return minimum2;
}
另一种是存储要查找的最小值的变量:
int a = 5, b = 7, c = 3;
并在main()
方法中有一条print
语句:
System.out.println("The minimum of " + a + ", " + b + " and " + c + " is " +
注意:
您可能会发现有趣的printf
,可以更灵活地使用它:
System.out.printf("The minimum of %d, %d and %d is %d.", a, b, c, minimum2);
System.out.printf("The minimum of %d, %d and %d is %d.", a, b, c, findMin(5, 7, 3));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.