【发布时间】:2019-10-27 19:50:00
【问题描述】:
我已经编写了一个 Java 程序,它完全按照它的预期工作,但是,答案需要四舍五入到小数点后 5 位。
我用谷歌搜索了很多,但我看到的每篇文章都有双重输入。这是一个需要四舍五入的 sumArea 答案。
public class COSC_HW13
{
// Main method
public static void main(String[] args)
{
// Create an array of four objects
GeometricObject[] array = {new Circle(5), new Circle(8),
new Rectangle(3, 4), new Rectangle(4, 2)};
// Display results
System.out.println("Total area of elements in array: "
+ sumArea(array));
}
// Returns the sum of the areas of
//all the geometric objects in an array
public static double sumArea(GeometricObject[] a)
{
double sum = 0;
for (int i = 0; i < a.length; i++)
{
sum += a[i].getArea();
}
return sum;
}
}
【问题讨论】: