2016-08-31 3 views
-5

나는 당신에게 묻고 싶은 질문이 있습니다. 자바 프로그래밍에 대한 저서에서 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

당신의 문제는 당신이 측면의 길이를 찾으려면, 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

감사합니다. 그런 식으로 생각하지 마십시오. :) –