编辑//我可能认为Programmr.com用于检查答案输出与预期输出的代码是错误的.因为这里的所有答案都具有几乎相同的公式,并且维基页面上关于英雄公式的公式与此处的答案相同.
在本练习中,完成"返回值"的功能.当你调用这个函数时,它应该使用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
Run Code Online (Sandbox Code Playgroud)
使用这个..苍鹭的形式


double s = (a + b + c)/2.0d;
double x = (s * (s-a) * (s-b) * (s-c));
double Area= Math.sqrt(x);
return Area;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8120 次 |
| 最近记录: |