第二章第十六题(几何:六边形面积)(Geometry: area of a hexagon)
-
2.16(几何:六边形面积)编写程序,提示用户输入六边形的边长,然后显示它的面积。计算六边形面积的公式是:
面积 =。
这里的s就是边长。下面是一个运行示例:
Enter the length of the side : 5.5
The area of the hexagon is 78.5918
2.16Geometry: area of a hexagon) Write a program that prompts the user to enter the side of a hexagon and displays its area. The formula for computing the area of a hexagon is
area =.
where s is the length of a side. Here is a sample run:
Enter the length of the side : 5.5
The area of the hexagon is 78.5918
-
参考代码:
package chapter02;
import java.util.Scanner;
public class Code_16 {
public static void main(String[] args) {
double LengthHexagonSide,AreaHexagon;
System.out.print("Enter the length of the side : ");
Scanner LenSideInput = new Scanner(System.in);
LengthHexagonSide = LenSideInput.nextDouble();
AreaHexagon = 3 * Math.pow(3,0.5) / 2 * Math.pow(LengthHexagonSide,2);
System.out.println("The area of the hexagon is " + AreaHexagon);
LenSideInput.close();
}
}
- 结果显示:
Enter the length of the side : 5.5
The area of the hexagon is 78.59180539343781
Process finished with exit code 0