梯形面积的计算公式为:(上底 + 下底) * 高 / 2。以下是使用Java编写的计算梯形面积的代码:
public class TrapezoidArea {
public static void main(String[] args) {
double top = 5; // 上底
double bottom = 10; // 下底
double height = 8; // 高
double area = calculateTrapezoidArea(top, bottom, height);
System.out.println("梯形面积为: " + area);
}
public static double calculateTrapezoidArea(double top, double bottom, double height) {
return (top + bottom) * height / 2;
}
}
在这个代码中,我们定义了一个名为calculateTrapezoidArea
的方法,它接收三个参数:上底、下底和高。然后使用梯形面积公式计算面积,并返回结果。在main
方法中,我们调用这个方法并输出结果。