[英]If statement seems to be skipping to else
我正在嘗試編寫一個程序,該程序確定圓是否在內部/是否接觸矩形。 用戶為圓和半徑輸入中心點,為矩形輸入兩個對角點。
我不確定如何包含圓的所有點,以判斷矩形/矩形中至少有一個點。 有人知道該怎么做嗎?
當我運行當前程序時,我將有目的地輸入位於矩形內部的圓的點,並且應與我放置的if語句一起使用,但它會打印出錯誤的答案。
import java.util.Scanner;
public class lab4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double cx, cy, x, y, r, p1x, p1y, p2x, p2y, max;//input
String a;
System.out.print("Enter cx: ");
cx = in.nextDouble();
System.out.print("Enter cy: ");
cy = in.nextDouble();
System.out.print("Enter r: ");
r = in.nextDouble();
System.out.println("Enter x value of point 1:");
p1x = in.nextDouble();
System.out.println("Enter y value of point 1:");
p1y = in.nextDouble();
System.out.println("Enter x value of point 2:");
p2x = in.nextDouble();
System.out.println("Enter y value of point 2:");
p2y = in.nextDouble();
max = p2x;
if (p1x > max)
max = p1x;
max = p2y;
if (p1y > max)
max = p1y;
if (cx >= p1x && cx <= p2x)
a = "Circle is inside of Rectangle";
if (cx >= p1x && cx <= p2x)
a = "Circle is inside of Rectangle";
if (cx+r >= p1x && cx+r <= p2x)
a = "Circle is inside of Rectangle";
if (cx-r >= p1x && cx-r <= p2x)
a = "Circle is inside of Rectangle";
if (cy >= p1y && cy <= p2y)
a = "Circle is inside of Rectangle";
if (cy >= p1y && cy <= p2y)
a = "Circle is inside of Rectangle";
if (cy+r >= p1y && cy+r <= p2y)
a = "Circle is inside of Rectangle";
if (cy-r >= p1y && cy-r <= p2y)
a = "Circle is inside of Rectangle";
else
a = "Circle is outside of Rectangle";
System.out.println(a);
您的else語句僅以最后一個if語句為條件。 因此,如果最后一個if語句為false,則執行else語句。 您可能想要:
if ...
else if ...
else if ...
else
僅當所有先前的“ if”語句為false時,才執行else。
因為您沒有為每個條件使用else if
,所以最后一對if
和else
語句對將覆蓋之前的所有if
。
if (cx >= p1x && cx <= p2x) a = "Circle is inside of Rectangle"; if (cx >= p1x && cx <= p2x) a = "Circle is inside of Rectangle"; if (cx+r >= p1x && cx+r <= p2x) a = "Circle is inside of Rectangle"; if (cx-r >= p1x && cx-r <= p2x) a = "Circle is inside of Rectangle"; if (cy >= p1y && cy <= p2y) a = "Circle is inside of Rectangle"; if (cy >= p1y && cy <= p2y) a = "Circle is inside of Rectangle"; if (cy+r >= p1y && cy+r <= p2y) a = "Circle is inside of Rectangle";
if (cy-r >= p1y && cy-r <= p2y)
a = "Circle is inside of Rectangle";
else
a = "Circle is outside of Rectangle";
對於每種選擇,請確保使用else if
,以確保僅執行其中一個塊。
if (cx >= p1x && cx <= p2x)
a = "Circle is inside of Rectangle";
else if (cx >= p1x && cx <= p2x)
a = "Circle is inside of Rectangle";
else if (cx+r >= p1x && cx+r <= p2x)
a = "Circle is inside of Rectangle";
else if (cx-r >= p1x && cx-r <= p2x)
a = "Circle is inside of Rectangle";
else if (cy >= p1y && cy <= p2y)
a = "Circle is inside of Rectangle";
else if (cy >= p1y && cy <= p2y)
a = "Circle is inside of Rectangle";
else if (cy+r >= p1y && cy+r <= p2y)
a = "Circle is inside of Rectangle";
else if (cy-r >= p1y && cy-r <= p2y)
a = "Circle is inside of Rectangle";
else
a = "Circle is outside of Rectangle";
(此更正將解決當前問題,但是整個算法仍然不正確。)
正如其他人所說,您需要一系列if ... else if ... else if ... else
才能使您的邏輯正常工作。
但是,有一種更簡單的方法。 要測試圓的任何部分是否接觸到矩形或在矩形內部,只需將矩形擴展圓的半徑,然后測試圓的中心是在擴展矩形的內部還是在擴展矩形上。 由於使用的是雙坐標,因此可以使用Rectangle.Double
來完成所有繁重的工作:
public static void main(String[] args) {
double cx, cy, r, p1x, p1y, p2x, p2y;
// first input cx, cy, r, p1x, p1y, p2x, and p2y
// construct a zero-width/height rectangle at p1
Rectangle2D.Double p1 = new Rectangle2D.Double(p1x, p1y, 0, 0);
// construct another one at p1
Rectangle2D.Double p2 = new Rectangle2D.Double(p2x, p2y, 0, 0);
// construct the union of the two
Rectangle2D.Double rect = p1.createUnion(p2);
// expand the rectangle
rect.setBounds(rect.x - r, rect.y - r, rect.w + 2 * r, rect.h + 2 * r);
// test for containment
if (rect.contains(cx, cy) {
a = "Circle is inside of Rectangle";
} else {
a = "Circle is outside of Rectangle";
}
System.out.println(a);
}
一些偽代碼:
將圓移到原點可簡化操作。 將矩形移位相同的數量。
p1x = p1x - cx
p2x = p2x - cx
p1y = p1y - cy
p2y - p2y - cy
x^2 + y^2 = r^2
y = +- sqrt( r^2 - x^2)
For x = -r to r
y = + sqrt( r^2 - x^2 )
if ( Inbounds(x,y) )return true;
y = - sqrt( r^2 - x^2 )
if ( Inbounds(x,y) )return true;
End For
為了提高精度,您可以執行以下操作:
對於x = -r至r步驟0.01(使用雙精度並遞增0.01)
因為您將每個情況分開處理,而不使用else if
,所以if條件是如果if條件為true則覆蓋a的值,則else if與最后一個if語句有關,而不是全部。
我建議每個串聯結果變量a
像這樣的,看看哪些條件是有效的:
if (cx >= p1x && cx <= p2x)
a += "Circle is inside of Rectangle \n";
if (cx >= p1x && cx <= p2x)
a += "Circle is inside of Rectangle\n";
if (cx+r >= p1x && cx+r <= p2x)
a += "Circle is inside of Rectangle\n";
if (cx-r >= p1x && cx-r <= p2x)
a += "Circle is inside of Rectangle\n";
if (cy >= p1y && cy <= p2y)
a += "Circle is inside of Rectangle\n";
if (cy >= p1y && cy <= p2y)
a += "Circle is inside of Rectangle\n";
if (cy+r >= p1y && cy+r <= p2y)
a += "Circle is inside of Rectangle\n";
if (cy-r >= p1y && cy-r <= p2y)
a += "Circle is inside of Rectangle\n";
else
a += "Circle is outside of Rectangle\n";
或者,如果這不是您想要的,則將else if添加到所有這樣的if語句中:
if (cx >= p1x && cx <= p2x)
a = "Circle is inside of Rectangle";
else if (cx >= p1x && cx <= p2x)
a = "Circle is inside of Rectangle";
else if (cx+r >= p1x && cx+r <= p2x)
a = "Circle is inside of Rectangle";
else if (cx-r >= p1x && cx-r <= p2x)
a = "Circle is inside of Rectangle";
else if (cy >= p1y && cy <= p2y)
a = "Circle is inside of Rectangle";
else if (cy >= p1y && cy <= p2y)
a = "Circle is inside of Rectangle";
else if (cy+r >= p1y && cy+r <= p2y)
a = "Circle is inside of Rectangle";
else if (cy-r >= p1y && cy-r <= p2y)
a = "Circle is inside of Rectangle";
else
a = "Circle is outside of Rectangle";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.