2013-04-22 110 views
0

我的老师让我找到我创建的两个矩形之间的交集。请帮我弄清楚为什么这个不运行。我得到的错误说找不到变量底部。矩形的交叉点java

public class Rectangle { 

    private int left, bottem, width, height; 

    public Rectangle (int l, int b, int w, int h) { 
     left = l; 
     bottem = b; 
     width = w; 
     height = h; 
    } 

    public int getX() { 
     return left; 
    } 

    public int getY() { 
     return bottem; 
    } 

    public int getW() { 
     return width; 
    } 

    public int getH() { 
     return height; 
    } 

    public int getArea() { 
     int area; 
     area = (width * height); 
     return area; 
    } 

    public int getPerimeter() { 
     int perimeter; 
     perimeter = (width + height) * 2; 
     return perimeter; 
    } 

    public int getIntersection (Rectangle one, Rectangle two) { 
     int intxValue; 
     int intyValue; 
     int intxValue2; 
     int intyValue2; 
     int area; 

     if (one.left + one.width > two.left && one.bottom + one.height > two.bottom) { 
       intxValue = two.left; 
       intyValue = two.bottom; 
       intxValue2 = one.left + one.width - intxValue; 
       intyValue2 = one.bottom + one.height - intyValue; 
       area = intxValue2*intyValue2; 
       return area; 
     } else if (one.left+one.width < two.left && one.bottom+one.height < two.bottom) { 
       intxValue = one.left; 
       intyValue = one.bottom; 
       intxValue2 = two.left + two.width - intxValue; 
       intyValue2 = two. bottom + two.height - intyValue; 
       area = intxValue2*intyValue2; 
       return area; 
     } else return area; 
    } 
+1

'bottem'与'bottom'不是一回事。 – 2013-04-22 19:38:28

+0

请向您要求帮助您的人免费赠送礼物,并让您的代码易于阅读。 – djechlin 2013-04-22 19:38:37

+0

哦,废话哈哈拼写错误:P – 2013-04-22 19:39:23

回答

1

因为在顶部,你已经写了:

bottem = b; 

不下。

下一次尝试通过格式化代码来更容易地帮助您。

+0

谢谢:)对不起,它很难阅读我试图在Mac上做到这一点,它很难格式化。在我的程序中没问题,但是当我粘贴它时,格式发生了变化。 – 2013-04-22 19:44:33