因此,我得到了一个项目,在这个项目中,我必须编写一个Java程序来返回三角形边之间的角,给定这三条边作为方法的参数。有一个数学公式,我们可以用它来导出一个叫做余弦定律的方程,它是给定三角形a,b,和C,c^2 = a^2 +b^2-2AB* cos(C)的边,其中C是边a和b之间的某个角度。我们可以分离出角c,得到方程arccos((-c^2 + a^2 + b^2) / 2ab) =C。这个数学很简单,在Java中很容易实现,但当我输入边3、4、5时,输出应该是90、30、60,我得到的角度是53.130102,36.86989,90.0。这些绝对不是像3,4,5这样的毕达哥拉斯三重角,有人知道我哪里出错了吗?
代码:
import static java.lang.Math.sqrt;
import static java.lang.Math.acos;
import static java.lang.Math.pow;
import static java.lang.Math.PI;
class Main {
public static void main(String[] args) {
anglesFinder(3, 4, 5);
}
public static void anglesFinder(int a, int b, int c) {
double alpha;
double beta;
double gamma;
alpha = (double) Math.acos((Math.pow(b, 2) + Math.pow(c, 2) - Math.pow(a, 2)) / (2 * c * b));
beta = (double) Math.acos((Math.pow(a, 2) + Math.pow(c, 2) - Math.pow(b, 2)) / (2 * a * c));
gamma = (double) Math.acos((Math.pow(a, 2) + Math.pow(b, 2) - Math.pow(c, 2)) / (2 * a * b));
System.out.println("angle between a & b is: " + (beta * (180 / Math.PI)));
System.out.println("angle between a & c is: " + (alpha * (180 / Math.PI)));
System.out.println("angle between b & c is: " + (gamma * (180 / Math.PI)));
}
}
发布于 2019-10-09 08:18:07
你的计划是正确的,但你是不正确的。对于长度(比例)长度为1、平方( 3 )/2(约0.866)和2的直角,则需要30、60和90度的直角,其长度不为3、4和5。
我的计算器给出的余弦约为53.130102度,余弦约为0.8度,与3、4和5相配。
https://stackoverflow.com/questions/58308232
复制