7

代码:

public void placeO(int xpos, int ypos) {
    for(int i=0; i<3;i++)
        for(int j = 0;j<3;j++) {
            // The line below does not work. what can I use to replace this?
            if(position[i][j]==' ') {
                position[i][j]='0';
            }
        }
}
4

3 回答 3

13

将其更改为:if(position[i][j] == 0)
每个 char 都可以与一个 int 进行比较。
默认值为'\u0000'ie0用于 char 数组元素。
我想这正是你的意思empty cell

要对此进行测试,您可以运行它。

class Test {

    public static void main(String[] args) {
        char[][] x = new char[3][3];
        for (int i=0; i<3; i++){
            for (int j=0; j<3; j++){
                if (x[i][j] == 0){
                    System.out.println("This char is zero.");
                }
            }
        }
    }

}
于 2014-02-01T19:59:37.590 回答
5

假设你已经初始化了你的数组,比如

char[] position = new char[length];

char每个元素的默认值'\u0000'是(空字符),它也等于0. 所以你可以检查这个:

if (postision[i][j] == '\u0000')

或者如果您想提高可读性,请使用它:

if (positionv[i][j] == 0)
于 2014-02-01T20:03:01.810 回答
1
 if(position[i][j]==0)
{
 // The index value of [i][j] is 0
}
于 2014-02-01T19:59:22.370 回答