gpt4 book ai didi

java - 如何在 Java 中使用切线?

转载 作者:行者123 更新时间:2023-12-01 11:37:10 25 4
gpt4 key购买 nike

我很无聊,想练习我的 Java 编码技能。我编写了一个程序,可以根据您所知道的(半径、周长、边心线)查找多边形的面积。

这是一部分:

static void pentagon() {
System.out.println("Select what you know");
System.out.println("[1]Perimeter\n[2]Apothem\n[3]Radius");
info = input.nextInt();

if (info == 1) {
System.out.println("What is the perimeter of the pentagon?");
double per = input.nextDouble();
double apothem = per * .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
} else if (info == 2) {
System.out.println("What is the apothem of the pentagon?");
double apothem = input.nextDouble();
double per = apothem / .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
} else if (info == 3) {
System.out.println("What is the radius of the pentagon?");
double rad = input.nextDouble();
double per = rad / .1701301617;
double apothem = per * .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
}
}

由于所有这些小数(边心与周长之比)我必须自己计算,所以我只能编写一些有用的小数。

如果我知道如何使用切线,我就能解决这个问题。

例如:双边心 = 长度/tan(360/10/2)

(十边形的边心)有人可以告诉我如何编写上一行代码吗?

最佳答案

推荐的方法是使用java.lang.Math.tan(double a)

double apothem = 1 / java.lang.Math.tan( (2*java.lang.Math.PI)/(10*2))

除非出于某种原因您需要非凡的精度,而这却无法提供。然后您也许可以找到一些第三方替代方案。

关于java - 如何在 Java 中使用切线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com