假設將一個直角三角形放置在一個平面上,如下所示。直角點放置在(0, 0)處,另外兩個點放置在(200, 0)和(0, 100)處。編寫一個程序,提示用戶輸入帶有 x 和 y 坐標的點,并確定該點是否在三角形內部。? ? `String xInput, yInput;? ? double x, y;? ? xInput = JOptionPane.showInputDialog("Enter the x-coordinate of the point");? ? yInput = JOptionPane.showInputDialog("Enter the y-coordinate of the point");? ? x = Double.parseDouble(xInput);? ? y = Double.parseDouble(yInput);? ? if (x <= 200 && x >= 0 && y <= 100 && y >= 0) {? ? ? ? if (y = roundup(x/2))? ? ? ? ? ? System.out.print("The point is in the the triangle");? ? ? ? else? ? ? ? ? ? System.out.print("The point isn't in the triangle");? ? }else? ? ? ? System.out.print("The point isn't in the triangle");`The output is an error in the second if saying that a double can't be a boolean
2 回答

阿晨1998
TA貢獻2037條經驗 獲得超6個贊
基本上你有一個線性公式 y = 100 - x/2 其中 x 在 0 到 200 之間,所以我們可以為此創建簡單的方法
static double calculateY(double x) {
return 100.0 - x / 2.0;
}
然后將 x 與邊界進行比較,將 y 與公式進行比較
if (x < 0 || x > 200 || y < 0) {
System.out.print("The point isn't in the triangle");
} else if ( y <= calculateY(x)) {
System.out.print("The point is in the the triangle");
} else
System.out.print("The point isn't in the triangle");
}

慕慕森
TA貢獻1856條經驗 獲得超17個贊
考慮一個頂點位于 (0, 0)、(1, 0) 和 (0, 1) 的三角形。編寫一個程序,詢問用戶 x 和 y 坐標,然后輸出該點是在三角形內部、在三角形邊界上還是在三角形外部。
添加回答
舉報
0/150
提交
取消