简体   繁体   English

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

[英]How to compare 2 elements of a two dimensional arrays in java?

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++;
    }

I am wanting to compare the contents in array[0][0] with array[0][1] and array[0][2] and they are all the same, they print that the place holder is the winner.我想将 array[0][0] 中的内容与 array[0][1] 和 array[0][2] 进行比较,它们都是相同的,它们打印出占位符是赢家。 When I run the program, the string "X" goes into the array, but I cannot get them to compare to each other.当我运行程序时,字符串“X”进入数组,但我无法让它们相互比较。 What method is best?什么方法最好? I have tried the .equals() method and the .compareto() method but I still can't get anything to work.我已经尝试了 .equals() 方法和 .compareto() 方法,但我仍然无法得到任何工作。

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

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

应该做的伎俩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM