簡體   English   中英

布爾方法的行為不符合預期,當預期為true時返回false

[英]boolean method does not behave as expected and returns false when true is expected

這是有問題的代碼:

public boolean validMoveRook(int[] start, int[] end) {

    int xDif = end[0]-start[0];
    int yDif = end[1]-start[1];

    if (xDif == 0 ^ yDif == 0) {

        int distance;
        int direction;

        if (xDif == 0) {
            distance = Math.abs(yDif);
            direction = 1;
        }
        else {
            distance = Math.abs(xDif);
            direction = 0;
        }

        for (int i = distance - 1; i >= 0; i--) {
            if (direction == 0) {
                int newX = start[0] + utilities.AbsoluteChange(xDif, -i);
                if (this.board[newX][start[1]] != FREE) return false;
            }
            if (direction == 1) {
                int newY = start[1] + utilities.AbsoluteChange(yDif, -i);
                if (this.board[start[0]][newY] != FREE) return false;
            }

            if (this.board[start[0]][start[1]] == TURN0_WHITEROOK) {
                this.board[start[0]][start[1]] = WHITEROOK;
            }
            if (this.board[start[0]][start[1]] == TURN0_BLACKROOK) {
                this.board[start[0]][start[1]] = BLACKROOK;
            }
        }
        System.out.println("success");
        return true;
    }
    System.out.println("Oh no!");
    return false;
}

我正在嘗試下棋游戲。 問題在於,即使不應該返回,它也會一直返回false。 打印“成功”,但它仍返回false。 為什么是這樣?

正如@Gblodgett所指出的,您的utilities.AbsoluteChange很可能有一個Sop("success") 。AbsoluteChange方法,並且在調用之后將返回false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM