2012-04-25 21 views
1

我只是得到錯誤的條件打印到小程序。我假設我的嵌套if語句有問題嗎?嵌套if語句。每次同樣的條件下

ClickableBox.Java

public class ClickableBox extends MouseAdapter { 

    private int x, y, width, height; 
    private Color borderColor, backColor, oldColor; 
    private boolean drawBorder, clicked, isX; 
    private Container parent; 
    TheGame game; 

    public ClickableBox(int x, int y, int width, int height, Color borderColor, 
     Color backColor, boolean drawBorder, TheGame parent) { 

    this.x = x; 
    this.y = y; 
    this.width = width; 
    this.height = height; 
    this.borderColor = borderColor; 
    this.backColor = backColor; 
    this.drawBorder = drawBorder; 
    this.parent = parent; 

    } 

    public void draw(Graphics g) { 

    oldColor = g.getColor(); 
    g.setColor(backColor); 
    g.fillRect(x, y, width, height); 
    if (drawBorder) { 
     g.setColor(borderColor); 
     g.drawRect(x, y, width, height); 
    } 
    g.setColor(oldColor); 
    } 

    public void mouseReleased(MouseEvent e) { 

    if (x < e.getX() && e.getX() < x + width && y < e.getY() 
     && e.getY() < y + height) { 
     clicked = true; 
     setX(!isX); 
     parent.repaint(); 

    } 
    } 

    public boolean isClicked() { 
    return clicked; 
    } 

    public int getX() { 
    return x; 
    } 

    public void setX(int x) { 
    this.x = x; 
    } 

    public int getY() { 
    return y; 
    } 

    public void setY(int y) { 
    this.y = y; 
    } 

    public int getWidth() { 
    return width; 
    } 

    public void setWidth(int width) { 
    this.width = width; 
    } 

    public int getHeight() { 
    return height; 
    } 

    public void setHeight(int height) { 
    this.height = height; 
    } 

    public Color getBorderColor() { 
    return borderColor; 
    } 

    public void setBorderColor(Color borderColor) { 
    this.borderColor = borderColor; 
    } 

    public Color getBackColor() { 
    return backColor; 
    } 

    public void setBackColor(Color backColor) { 
    this.backColor = backColor; 
    } 

    public boolean isDrawBorder() { 
    return drawBorder; 
    } 

    public void setDrawBorder(boolean drawBorder) { 
    this.drawBorder = drawBorder; 
    } 

    public boolean isX() { 
    return isX; 
    } 

    public void setX(boolean isX) { 
    this.isX = isX; 
    } 

} 

TicTacToeBox.Java

public class TicTacToeBox extends ClickableBox { 

    Container parent; 
    TheGame game; 

    public TicTacToeBox(int x, int y, int width, int height, Color borderColor, 
     Color backColor, boolean drawBorder, TheGame parent) { 
    super(x, y, width, height, borderColor, backColor, drawBorder, parent); 

    this.parent = parent; 

    } 

    public void draw(Graphics g) { 

    if (isClicked()) { 
     if (super.isX()) { 
     g.drawLine(getX(), getY(), getX() + getWidth(), getY() + getHeight()); 
     g.drawLine(getX() + getWidth(), getY(), getX(), getY() + getHeight()); 
     if (isDrawBorder()) { 
      g.drawRect(getX(), getY(), getWidth(), getHeight()); 
     } 
     } else { 
     g.drawOval(getX() + 3, getY() + 3, getWidth() - 6, getHeight() - 6); 
     if (isDrawBorder()) { 
      g.drawRect(getX(), getY(), getWidth(), getHeight()); 
     } 
     } 
    } else { 
     g.drawRect(getX(), getY(), getWidth(), getHeight()); 
    } 
    } 

} 

下面是實際的applet代碼,以及... TheGame.java

public class TheGame extends Applet { 

    private final int START_X = 20; 
    private final int START_Y = 40; 
    private final int ROWS = 3; 
    private final int COLS = 3; 
    private final int BOX_WIDTH = 70; 
    private final int BOX_HEIGHT = 70; 

    private TicTacToeBox boxes[][]; 

    private Button resetButton; 
    private boolean isX; 

    private boolean blank; 

    public void init() { 
    boxes = new TicTacToeBox[ROWS][COLS]; 

    resize(300, 300); 
    buildBoxes(); 

    resetButton = new Button("Reset Game"); 
    resetButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

     buildBoxes(); 
     repaint(); 
     } 
    }); 
    add(resetButton); 
    } 

    public void paint(Graphics g) { 
    // loop through the boxes rows 

    setX(!isX); 
    // System.out.println(isX()); 

    for (int row = 0; row < boxes.length; row++) { 
     for (int col = 0; col < boxes[row].length; col++) { 

     boxes[row][col].draw(g); 

     if (boxes[row][col].isClicked()) { 

     } 
     } 
    } 

    } 

    private void buildBoxes() { 

    for (int row = 0; row < boxes.length; row++) { 
     for (int col = 0; col < boxes[row].length; col++) { 
     boxes[row][col] = new TicTacToeBox(START_X + col * BOX_WIDTH, START_Y 
      + row * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT, Color.black, 
      Color.white, true, this); 
     addMouseListener(boxes[row][col]); 

     } 
    } 
    } 

    public boolean isX() { 
    return isX; 
    } 

    public void setX(boolean isX) { 
    this.isX = isX; 
    } 

    public boolean isBlank() { 
    return blank; 
    } 

    public void setBlank(boolean blank) { 
    this.blank = blank; 
    } 

} 

任何輸入至於如何我可以得到從真到假交替的條件,並實際輸出我對每種情況的判斷d不勝感激。

+1

代碼的縮進是不一致的 - 例如,行說'如果(isDrawBorder())'它的縮進比前一個縮小了,儘管這裏沒有大括號,最後幾個大括號似乎與他們的合作伙伴不匹配。如果你解決了這個問題,讀者會更容易知道你的意圖(並且因此告訴你的代碼是否與你的意圖不符)。 – 2012-04-25 13:49:02

+4

不要使用if(something == true){} else if(something == false){}。只要使用if(something){} else {}。布爾值是真或假,寫入==真正是多餘的,可讀性較差。 – 2012-04-25 13:49:15

+1

修正了代碼中的一些顯而易見的東西,比如不將布爾值與真或假進行比較,並首先測試正面結果,然後測試結果爲負面。更易於閱讀。現在,使用這段代碼,看看你能否找到錯誤。代碼很簡單。 – 2012-04-25 13:49:47

回答

5

你不斷重新演繹TheGame,這就是爲什麼你總是得到你的if分支。 game應該成爲你的會員封閉類不是一個局部變量,你應該initalize它在構造函數:

public class TicTacToeBox { 
    private TheGame game; 

    // I am guessing your constructor is something like this (but it is just guessing) 
    public TicTacToeBox(int i, int j , int k , int l, Color c1, Color c2, boolean b, TheGame game) { 
     ... 
     this.game = game; 
    } 
    ... 
    public void draw(Graphics g) { 
     // TheGame game = new TheGame(); 
     ... 
    } 
+0

謝謝,但我仍然每次都會畫出橢圓形。它不像它應該交替。問題可能出現在TheGame.Java代碼中。 – MontyTheMack 2012-04-25 14:00:20

+1

@MontyTheMack好的,現在我明白了,你的繪圖方法在你的TicTacToeBox類中。所以你的TicTacToeBox類應該通過構造函數從TheGame得到一個引用,並保留該引用 – 2012-04-25 14:07:18

+0

@MontyTheMack如果你已經改變了你的代碼中的任何東西,然後更新你的問題。你認爲我們可以幫助你嗎? **你在哪裏存儲你的TheGame實例?我們沒有看到。 – 2012-04-25 14:09:16