【问题标题】:If statement seems to be skipping to elseif 语句似乎跳到 else
【发布时间】:2013-09-13 20:44:40
【问题描述】:

我正在尝试编写一个程序来确定一个圆是否在一个矩形内/是否接触到一个矩形。用户输入圆的中心点和半径,以及矩形的两个对角点。

我不确定如何包含圆的圆周上的所有点,以说明至少有一个点在/接触矩形。有人知道怎么做吗?

当我运行我当前的程序时,我会故意输入一个矩形内的圆点,并且应该使用我输入的 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,而不适用于所有,也许您覆盖了一个值,相反,我建议 将每个结果连接到变量 a 并查看所有结果

标签: java if-statement geometry rectangles


【解决方案1】:

您的 else 语句仅以最后一个 if 语句为条件。因此,如果最后一个 if 语句为 false,则执行 else 语句。你可能想要:

if ...
else if ...
else if ...
else

仅当所有先前的“if”语句为假时才执行else。

【讨论】:

    【解决方案2】:

    因为您没有对每个条件都使用else if,所以最后一对ifelse 语句将覆盖之前的所有ifs。

    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";
    

    (此更正将解决当前的问题,但整个算法仍然不正确。)

    【讨论】:

      【解决方案3】:

      正如其他人所说,您需要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);
      }
      

      【讨论】:

        【解决方案4】:

        一些伪代码:

        将圆圈移到原点以使事情更简单。 将矩形移动相同的量。

        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)

        【讨论】:

          【解决方案5】:

          因为你在没有else if 的情况下单独处理每个案例,所以如果条件是你覆盖 a 的值,如果 if 条件为真,你的 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";
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-06-10
            • 1970-01-01
            • 2017-09-20
            • 2018-01-11
            • 1970-01-01
            • 2022-06-10
            • 1970-01-01
            相关资源
            最近更新 更多