2016-08-31 14 views
-5

私はあなたに尋ねたい質問があります。私のJavaプログラミングの本では、3点与えられた三角形の領域を見つけるプログラムを書くように頼まれています。私は多くの方法を試みましたが、私は決して正しい答えを得ることができませんでした。あなたは私にこの問題の解決策を教えてください。ありがとう!ここで質問です:あなたは正しく側面を発見されていないため、三角の領域を見つける

import java.util.Scanner; 

public class shw2point15 { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     System.out.println("Enter three points for a triangle:"); 

     double x1 = input.nextDouble(); 
     double y1 = input.nextDouble(); 
     double x2 = input.nextDouble(); 
     double y2 = input.nextDouble(); 
     double x3 = input.nextDouble(); 
     double y3 = input.nextDouble(); 


     double s = ((x1 + y1) + (x2 + y2) + (x3 + y3))/2; 
     double area = Math.sqrt(s * (s - (x1 - y1)) * (s - (x2 - y2)) * (s - (x3 - y3))); 



     System.out.println("The area of the triangle is " + area); 
    } 
} 
+0

私はあなたの問題は、あなたが、Y1にX1を追加する辺の長さを見つけるために、私はそれが別の方法で行われていると思いますが、私は助けることができないので、私は幾何学的に失敗したということだと思います –

+0

これはあなたですか? http://programmers.stackexchange.com/questions/329771/finding-area-of-a-triangle-using-equations – Blorgbeard

+1

* 3 * points; 6数字。私はそれに少し戸惑いました。 –

答えて

0

あなたは正しい答えを取得していない理由は次のとおりです。

2.15

はここに私のコードです。しかし、あなたは答えを得ることができる側の長さを見つけた後。ここに私がやったことです:

public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 

     System.out.println("Enter three points for a triangle:"); 

     //Store the values in an array. 
     double[] xCoordinates = new double[3]; 
     double[] yCoordinates = new double[3]; 
     double[] sides = new double[3]; 

//  Input the values into the array 
     xCoordinates[0] = input.nextDouble(); 
     yCoordinates[0] = input.nextDouble(); 
     xCoordinates[1] = input.nextDouble(); 
     yCoordinates[1] = input.nextDouble(); 
     xCoordinates[2] = input.nextDouble(); 
     yCoordinates[2] = input.nextDouble(); 

//  Find the side length from the input. There probably are better ways to do this. 
     sides[0] = Math.sqrt(Math.pow(xCoordinates[0]-xCoordinates[1], 2)+Math.pow(yCoordinates[0]-yCoordinates[1], 2)); 
     sides[1] = Math.sqrt(Math.pow(xCoordinates[1]-xCoordinates[2], 2)+Math.pow(yCoordinates[1]-yCoordinates[2], 2)); 
     sides[2] = Math.sqrt(Math.pow(xCoordinates[2]-xCoordinates[0], 2)+Math.pow(yCoordinates[2]-yCoordinates[0], 2)); 

//  Find s from the sides 
     double s = (sides[0]+sides[1]+sides[2])/2; 

//  Find the area. 
     double area = Math.sqrt(s*(s-sides[0])*(s-sides[1])*(s-sides[2])); 

//  Print the area 
     System.out.println("The area of the triangle is "+area); 

//  Output~~~~~~~~~~~~~~~ 
//Enter three points for a triangle: 
//  1.5 
//  -3.4 
//  4.6 
//  5 
//  9.5 
//  -3.4 
//  The area of the triangle is 33.600000000000016 
}' 
+0

ありがとうございました。 :) –

関連する問題