【问题标题】:Nested if statement. Following same condition every time嵌套 if 语句。每次都遵循相同的条件
【发布时间】:2012-04-25 13:44:16
【问题描述】:

我只是得到了打印到小程序的错误条件。我假设我的嵌套 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;
  }

}

井字游戏.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());
    }
  }

}

这也是实际的小程序代码... 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;
  }

}

任何关于我如何让条件从真到假交替并实际输出我对每个条件的信息的输入将不胜感激。

【问题讨论】:

  • 代码的缩进不一致——例如,if(isDrawBorder()) 行的缩进小于其前身,即使那里没有右大括号,并且有几个右大括号位于结局似乎与他们的伙伴格格不入。如果您解决了这个问题,读者将更容易说出您的意图(从而判断您的代码是否与您的意图不符)。
  • 不要使用 if(something == true) {} else if (something == false){}。只需使用 if(something){}else{}。布尔值要么是真要么是假,写== true是多余的,可读性较差。
  • 修复了代码中一些明显的问题,例如不将布尔值与真假进行比较,并先测试阳性结果,然后测试阴性结果。更容易阅读。现在,使用这段代码,看看你是否能找到问题所在。代码很简单。
  • 因此,通过这些小修复,我仍然遇到一个问题,即当我单击每个框时,即使条件为真,它也会打印一个椭圆而不是 X。
  • 如果它画一个椭圆,那么isX返回false。添加System.out.println 说服自己。

标签: java tic-tac-toe


【解决方案1】:

您不断地重新实例化 TheGame,这就是为什么您总是得到相同的 if 分支。 game 应该是你封闭类的成员而不是局部变量,你应该在你的构造函数中初始化它:

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();
        ...
    }

【讨论】:

  • 谢谢,但我每次都画椭圆形。它不是应有的交替。问题可能出在 TheGame.Java 代码中......
  • @MontyTheMack 好的,我现在更好了,你的 draw 方法在你的 TicTacToeBox 类中。所以你的 TicTacToeBox 类应该通过构造函数从 TheGame 获取引用并保留该引用
  • @MontyTheMack 向我们展示您的井字游戏和鼠标监听器的完整代码。我们在浪费时间试图猜测您在做什么而不了解您的目标
  • @MontyTheMack 因此,在您的 mouseReleased 中,您应该将 isX(ClickableBox 之一)初始化为 theGame.isX(),然后切换 TheGame 的 isX 值。 (停止在任何地方实例化 TheGame!)。在您的绘制方法中,使用 ClickableBox 的 isX,而不是 TheGame 的 isX。最后,在 ClickableBox 和 TicTacToeBox 的构造函数中,将父对象的类型从 Container 更改为 TheGame。现在不要使用游戏,而是使用 parent,因为它是 TheGame 参考。
  • 在您的 mouseReleased 中,您忘记切换您的父母的状态并将Container parent 更改为TheGame parent!你应该有这个:setX(!parent.isX()); parent.setX(!parent.isX()); 而不是 setX(!isX);
猜你喜欢
  • 2019-10-14
  • 2017-10-29
  • 2012-02-17
  • 2019-04-04
  • 1970-01-01
  • 2013-12-22
  • 2017-12-24
  • 1970-01-01
  • 2015-10-01
相关资源
最近更新 更多