ste*_*816 5 java loops compare char charat
码:
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';
}
}
}
Run Code Online (Sandbox Code Playgroud)
将其更改为:if(position[i][j] == 0)
每个char都可以与int进行比较.对于char数组元素
,默认值为'\u0000'ie 0.
我认为这就是你的意思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.");
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
假设你已经初始化了你的数组
char[] position = new char[length];
Run Code Online (Sandbox Code Playgroud)
每个char元素的默认值是'\u0000'(空字符),它也等于0。所以你可以检查这个:
if (postision[i][j] == '\u0000')
Run Code Online (Sandbox Code Playgroud)
或者如果您想提高可读性,请使用它:
if (positionv[i][j] == 0)
Run Code Online (Sandbox Code Playgroud)