编辑// 我可能认为 Programmr.com 用于检查答案输出与预期输出的代码是错误的。因为这里所有的答案都有几乎相同的公式,而且wiki页面上关于英雄公式的公式也与这里的答案相同。
在本练习中,完成“返回值”的功能。当你调用这个函数时,它应该使用 Heron 公式计算三角形的面积并返回它。
海伦公式:面积 = (s*(sa) (sb) (sc))0.5 其中 s = (a+b+c)/2
我写了这个,但它似乎不正确,我无法弄清楚出了什么问题。这个的输出给出了错误的值:
public class Challenge
{
public static void main( String[] args )
{
double a;
a = triangleArea(3, 3, 3);
System.out.println("A triangle with sides 3,3,3 has an area of:" + a);
a = triangleArea(3, 4, 5);
System.out.println("A triangle with sides 3,4,5 has an area of:" + a);
a = triangleArea(9, 9, 9); // ! also realize the 9,9,9 is not even the same as the comment bellow. This was generated by the Programmr.com exercise.
System.out.println("A triangle with sides 7,8,9 has an area of:" + a );
}
public static double triangleArea( int a, int b, int c )
{
double s = (a + b + c)/2;
double x = ((s) * (s-a) * (s-b) * (s-c));
double Area = Math.sqrt(x);
return Area;
}
}
Expected Output
3.897114317029974
6.0
35.074028853269766
Your code's output
2.0
6.0
28.844410203711913