EDIT // Programmr.comが回答出力と期待される出力をチェックするために使用するコードが間違っていると思うかもしれません。ここの回答はすべてほぼ同じ式を持っているため、ヒーローの式に関するwikiページの式もここの回答と同じです.
この演習では、「値を返す」関数を完成させます。この関数を呼び出すと、ヘロンの公式を使用して三角形の面積を計算し、それを返す必要があります。
ヘロンの式: 面積 = (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