如何在java中比较二维数组的2个元素?

时间:2014-11-14 22:30:57

标签: java arrays multidimensional-array

int turnCounter = 0;
public String [][] board = new String[3][3];


    public static int buttonClicked = 0;

    //this is going to check if there is a winning move
    public String winningCheck(){
        if (board[0][0] ==(board[0][1]) 
                && board[0][0]==(board[0][2])){
            message = (board[0][0] + " is the winner");
            gameOver = true;
        }
        return message;
    }


if (xTurn == true && twoIsChosen == false && gameOver == false){
        board[0][1] = "X";
        b01.setText(board[0][1]);
        if(turnCounter > 3){
            winningCheck();
            notification.setText(message);
        }
        xTurn = false;
        notification.setText("O, it's your turn.");
        twoIsChosen = true;
        turnCounter++;
    }
if (xTurn == true && threeIsChosen == false && gameOver == false){
        board[0][2] = "X";
        b02.setText(board[0][2]);
        if(turnCounter > 3){
            winningCheck();
            notification.setText(message);
        }
        xTurn = false;
        notification.setText("O, it's your turn.");
        threeIsChosen = true;
        turnCounter++;
    }
if (xTurn == true && oneIsChosen == false && gameOver == false){
        board[0][0] = "X";
        b00.setText(board[0][0]);
        if(turnCounter > 3){
            winningCheck();
            notification.setText(message);
        }
        xTurn = false;
        notification.setText("O, it's your turn.");
        oneIsChosen = true;
        turnCounter++;
    }

我想比较数组[0] [0]中的内容与数组[0] [1]和数组[0] [2]并且它们都是相同的,它们打印出占位符是胜利者。当我运行程序时,字符串" X"进入阵列,但我不能让他们相互比较。什么方法最好?我已经尝试过.equals()方法和.compareto()方法,但我仍然无法使用任何东西。

2 个答案:

答案 0 :(得分:0)

对于字符串比较,使用string1.equals(string2)优于相等比较

答案 1 :(得分:0)

if (board[0][0].equals(board[0][1]) && board[0][0].equals(board[0][2]))

应该做的伎俩