当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Shape Calculator Pentagon and Hexagon Perimeter and Area

问题描述:

Sorry to trouble but it seems I'm a little lost.

I am currently creating a Shape Calculator for 2D and 3D shapes and I seem to be having a problem with the above mentioned shapes in the title.

Now I have gone about trying to use this particular section of code to get the Area of my Pentagon, I've seen this work elsewhere but can't figure out why it won't work here even after reviewing and comparing my code? I thought someone could possibly point out if this is the correct way to go about solving it or if I've made a mistake I can't see myself? Generally need a second opinion sorry.

double pen = scan.nextDouble();

double penPerm = pen * 5;

double A1 = pen * Math.sqrt(5);

double A2 = 5 + A1;

double A3 = Math.sqrt(5 * A2);

double PenA = (1.0 / 4.0) * A3 * Math.pow(pen, 2);

System.out.println("Your Perimitre is :" + penPerm + "cm and your Area is :" + PenA + "cm Squared");

网友答案:

For a regular pentagon you can try the following code:

public static void main(String[] args) {
    double side = 10;
    double area = (1.0/4.0) * Math.sqrt(5*(5+2*Math.sqrt(5))) * Math.pow(side,2);
    System.out.println("Your Perimitre is :" + 5*side + "cm and your Area is :" + area + "cm Squared");
}

You can also try the following code which takes no of sides (n) and edge size (s) as input and computes area for a regular polygon:

     public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println(" Enter the number of sides in polygon");
        int n = input.nextInt();

        System.out.println(" Enter the distance between two points");
        double s = input.nextDouble();
        double area = (n * Math.pow(s, 2)) / (4 * Math.tan(Math.PI / n));

        //Print result
        System.out.println (" Area is " + area);
        System.out.println (" Perimeter is " + s*n);

    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: