2014-11-14 161 views
-1
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()方法,但我仍然無法獲得任何工作。如何比較java中的二維數組的2個元素?

+0

字符串是** **不相比較''==(除非你需要比較的身份。使用['equals()'](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object))方法代替 – 2014-11-14 22:35:15

+1

可能的重複[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – 2014-11-14 22:35:51

+0

您是否嘗試過調試應用程序? – 2014-11-14 22:43:01

回答

0

的字符串比較使用string1.equals(string2)大於等於比較

+0

好嗎,那麼可以使用board [0] [0] .equals(board [0] [1])工作嗎? – kurtdawg24 2014-11-14 22:41:35

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

更好的應該做的伎倆