Java 类型Int和字符串不兼容

Java 类型Int和字符串不兼容,java,string,int,incompatibility,Java,String,Int,Incompatibility,嘿,我完成了这个tic-tac-toe项目,我的checkWin方法中的board类有一个错误,winner=board[0][i];显示为Int和String的不兼容错误。我用Integer.toString()命令在我的另一个线路板上修复了这个问题,但它对这个问题不起作用。有什么想法吗?下面是checkWin方法的代码 public boolean checkWin() { { int i; // i = column int j; // j = r

嘿,我完成了这个tic-tac-toe项目,我的checkWin方法中的board类有一个错误,winner=board[0][i];显示为Int和String的不兼容错误。我用Integer.toString()命令在我的另一个线路板上修复了这个问题,但它对这个问题不起作用。有什么想法吗?下面是checkWin方法的代码

public boolean checkWin()
{
    {
        int i;  // i = column
        int j; // j = row
        int count; 
        int winner; 

          winner = empty; // nobody has won yet

// Check all rows to see if same player has occupied every square.

        for (j = 0; j < boardSize; j ++)
{
        count = 0;
    if (board[j][0] != Integer.toString(empty))

    for (i = 0; i < boardSize; i ++)
    if (board[j][0] == board[j][i])
    count ++;
    if (count == boardSize)
    winner = (board[j][0]);
}

// Check all columns to see if same player has occupied every square.

    for (i = 0; i < boardSize; i ++)
{
    count = 0;
    if (board[0][i] != Integer.toString(empty))
    for (j = 0; j < boardSize; j ++)
    if (board[0][i] == board[j][i])
    count ++;
    if (count == boardSize)
    winner = board[0][i];
}

// Check diagonal from top-left to bottom-right.

    count = 0;
    if (board[0][0] != Integer.toString(empty))
    for (j = 0; j < boardSize; j ++)
    if (board[0][0] == board[j][j])
    count ++;
if (count == boardSize)
winner = board[0][0];

// Check diagonal from top-right to bottom-left.

count = 0;
if (board[0][boardSize-1] != Integer.toString(empty))
for (j = 0; j < boardSize; j ++)
if (board[0][boardSize-1] == board[j][boardSize-j-1])
count ++;
if (count == boardSize)
winner = board[0][boardSize-1];

// Did we find a winner?

if (winner != empty)
{
if (winner == Xstr)

System.out.println("\nCongratulations! P1 You win!");
else if (winner == Ostr)

System.out.println("\nCongratulations! P2 You win!");
else


return true; 
}



}   
public boolean checkWin()
{
{
int i;//i=column
int j;//j=行
整数计数;
int优胜者;
winner=空;//还没有人赢
//检查所有行,查看是否有同一玩家占据了每个方块。
对于(j=0;j
winner
是一种int原语类型,
board
是多维字符串数组

您试图为int分配字符串,因此int和string不兼容错误

String[][] board;  
索引处有字符串,当您尝试访问
board[0][i]
时,您正在检索字符串

如果电路板数组包含数字的字符串表示形式,如

      boards=  {{"1"},{"2"}};
然后使用Integer.parseInt(),它将字符串作为参数并返回一个整数

winner = Integer.parseInt(board[0][i]);

但请注意,如果传递给parseInt的字符串不是字符串的有效整数表示形式,它将抛出NumberFormatException

您的board[][]类型是什么?要在类的顶部澄清,board是私有字符串[][]线路板;用于将字符串值转换为整数。此外,您正在将字符串与
==
进行比较,而不是与
.equals()
进行比较。它不会按您希望的方式工作。
String[][] board;