我一直在這個小時,嘗試不同的方法來看待每個問題。也許我完全錯了,但我覺得我的數學是正確的,但無論輸入什麼數字,我都會得到相同的輸出。我的代碼已關閉,我必須在午夜之前將其關閉。尋找一個點是否在一個三角形內
這是非常有趣的:找到一個點是否在三角形代碼內。 (初學者)
import java.util.Scanner;
public class PointsTriangle {
// checks if point entered is within the triangle
//given points of triangle are (0,0) (0,100) (200,0)
public static void main (String [] args) {
//obtain point (x,y) from user
System.out.print("Enter a point's x- and y-coordinates: ");
Scanner input = new Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
//find area of triangle with given points
double ABC = ((0*(100-0 )+0*(0 -0)+200*(0-100))/2.0);
double PAB = ((x*(0 -100)+0*(100-y)+0 *(y- 0))/2.0);
double PBC = ((x*(100-0 )+0*(0 -y)+200*(y-100))/2.0);
double PAC = ((x*(0 -100)+0*(100-y)+200*(y- 0))/2.0);
boolean isInTriangle = PAB + PBC + PAC == ABC;
if (isInTriangle)
System.out.println("The point is in the triangle");
else
System.out.println("The point is not in the triangle");
}//end main
}//end PointsTriangle
這可能是值得輸出你認爲你作爲調試的一部分讀值... – Floris