【问题标题】:Shape Calculator Pentagon and Hexagon Perimeter and Area形状计算器五边形和六边形周长和面积
【发布时间】:2015-06-29 10:58:32
【问题描述】:

很抱歉打扰了,但我好像有点迷路了。

我目前正在为 2D 和 3D 形状创建一个形状计算器,但标题中的上述形状似乎存在问题。

现在我已经开始尝试使用这段特定的代码来获取我的五角大楼的区域,我在其他地方看到过这项工作,但即使在查看和比较我的之后也无法弄清楚为什么它不能在这里工作代码?我想有人可能会指出这是否是解决问题的正确方法,或者如果我犯了一个错误,我看不到自己?一般需要第二个意见抱歉。

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");

另一个问题我应该如何解决六边形,但老实说,上述五角形问题是我在进入六边形之前主要关心的问题。

【问题讨论】:

  • 我假设你的五边形是规则的,并提出了一个适用于多边形的解决方案。

标签: java math calculator shape


【解决方案1】:

对于正五边形,您可以尝试以下代码:

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");
}

您也可以尝试以下代码,它不使用边 (n) 和边大小 (s) 作为输入并计算正多边形的面积:

     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);

    }

【讨论】:

    猜你喜欢
    • 2017-11-21
    • 2016-01-17
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 2013-03-25
    相关资源
    最近更新 更多