Chapter 6 Question 36 (Geometry: area of a regular polygon)

Chapter 6 Question 36 (Geometry: area of ​​a regular polygon)

  • *6.36 (Geometry: the area of ​​a regular polygon) A regular polygon is a polygon with n sides, each of which has the same length and the angles of all corners (that is, the polygon is both equilateral and equiangular). The formula for calculating the area of ​​a regular polygon is:
    Insert picture description here
    use the following method header to write the method to return the area of ​​the regular polygon:
    public static double area(int n, double side)
    Write a main method that prompts the user to enter the number of sides and the sides of the regular polygon Long, and then display its area.
    Here is a running example:
    Enter the number of sides: 5
    Enter the side:6.5
    The area of ​​the polygon is 72.690170
    *6.36(Geometry: area of ​​a regular polygon) A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (ie, the polygon is both equilateral and equiangular). The formula for computing the area of ​​a regular polygon is
    Insert picture description here
    Write a method that returns the area of ​​a regular polygon using the following header:
    public static double area(int n, double side)
    Write a main method that prompts the user to enter the number of sides and the side of a regular polygon and displays its area.
    Here is a sample run:
    Enter the number of sides: 5
    Enter the side:6.5
    The area of the polygon is 72.690170
  • Reference Code:
package chapter06;

import java.util.Scanner;

public class Code_36 {
    
    
    public static void main(String[] args) {
    
    
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Enter the number of sides: ");
        int numberOfSide = inputScanner.nextInt();
        System.out.print("Enter the side: ");
        double side = inputScanner.nextDouble();
        double area = area(numberOfSide, side);
        System.out.printf("The area of the polygon is %f", area);
    }
    public static double area(int n, double side) {
    
    
        double area = (n * Math.pow(side, 2)) / (4 * Math.tan(Math.PI / n));
        return area;
    }
}

  • The results show that:
Enter the number of sides: 5
Enter the side: 6.5
The area of the polygon is 72.690170
Process finished with exit code 0

Guess you like

Origin blog.csdn.net/jxh1025_/article/details/109230246