Fillgrid 方法在 Tic-Tac-Toe 程序中不起作用
Fillgrid method not working in Tic-Tac-Toe program
我正在为我的 AP 计算机科学编写一个井字棋程序 class 但是 fillgrid 方法(这意味着将板上每个 space 的初始值设置为“*”) 不是 运行。 Eclipse 说 String 与 char 不匹配,但我的老师告诉我使用 char。有什么建议吗?
节目:
public class tictacotoe {
public static void main(String[] args) {
char[][] grid = new char[3][3];
char player1 = 'X', player2 = 'O';
char currentPlayer = player1;
int turn = 0;
}//end main
public static void fillGrid(char[][] g) { //This will make the initial grid that it prints out
for(int row=0; row < g.length; row++) {
for(int col=0; col < g[0].length; col++) {
g[row][col] = "*";
}
}
}
public void printGrid(char[][] g) { //This will print the grid that you made above
/*
String output = "";
for(int row = 0; row < grid.length; col++) {
output += grid[row][col];
if(col != grid[0].length-1) output += "|";
}
if(row != grid.length-1) output += "\n--------------\n";
return output;
*/
}
}//end tictacotoe
不要在 g[row][col] = "*";
中分配字符串,而是尝试分配字符 g[row][col] = '*';
(单引号 ' 而不是双引号 ")。
只需将此 g[row][col] = "*";
更改为此 g[row][col] ='*';
。
如果您对此有任何疑问,请阅读此内容:Different between single and double quotes
我正在为我的 AP 计算机科学编写一个井字棋程序 class 但是 fillgrid 方法(这意味着将板上每个 space 的初始值设置为“*”) 不是 运行。 Eclipse 说 String 与 char 不匹配,但我的老师告诉我使用 char。有什么建议吗?
节目:
public class tictacotoe {
public static void main(String[] args) {
char[][] grid = new char[3][3];
char player1 = 'X', player2 = 'O';
char currentPlayer = player1;
int turn = 0;
}//end main
public static void fillGrid(char[][] g) { //This will make the initial grid that it prints out
for(int row=0; row < g.length; row++) {
for(int col=0; col < g[0].length; col++) {
g[row][col] = "*";
}
}
}
public void printGrid(char[][] g) { //This will print the grid that you made above
/*
String output = "";
for(int row = 0; row < grid.length; col++) {
output += grid[row][col];
if(col != grid[0].length-1) output += "|";
}
if(row != grid.length-1) output += "\n--------------\n";
return output;
*/
}
}//end tictacotoe
不要在 g[row][col] = "*";
中分配字符串,而是尝试分配字符 g[row][col] = '*';
(单引号 ' 而不是双引号 ")。
只需将此 g[row][col] = "*";
更改为此 g[row][col] ='*';
。
如果您对此有任何疑问,请阅读此内容:Different between single and double quotes