2013-04-22 157 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