用JAVA编写计算一个长方体的面积,定义有返回值,有参数的方法
时间: 2023-06-15 07:06:44 浏览: 54
可以使用以下代码来计算长方体的面积:
```java
public class Main {
public static void main(String[] args) {
double length = 5.0;
double width = 3.0;
double height = 2.0;
double area = calculateArea(length, width, height);
System.out.println("The area is: " + area);
}
public static double calculateArea(double length, double width, double height) {
double area = 2 * (length * width + length * height + width * height);
return area;
}
}
```
在这个例子中,我们定义了一个 `calculateArea` 方法来计算长方体的面积,它接受三个参数:长、宽和高。方法中使用公式 `2 * (length * width + length * height + width * height)` 计算出长方体的表面积,并将结果返回。在 `main` 方法中,我们设置长、宽和高的值,并调用 `calculateArea` 方法来计算长方体的面积,并将结果打印出来。