【问题标题】:Fillgrid method not working in Tic-Tac-Toe programFillgrid 方法在井字游戏程序中不起作用
【发布时间】:2020-04-26 18:42:17
【问题描述】:

我正在为我的 AP 计算机科学课程编写井字游戏程序,但 fillgrid 方法(用于将板上每个空间的初始值设置为“*”)没有运行。 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] = '*'; -- char 使用单引号,而不是双引号

标签: java arrays tic-tac-toe


【解决方案1】:

不要在g[row][col] = "*"; 中分配字符串,而是尝试分配一个字符g[row][col] = '*';(单引号'而不是双引号')。

【讨论】:

  • 是的,这就是问题所在。谢谢!
【解决方案2】:

只需将此g[row][col] = "*"; 更改为此g[row][col] ='*';

如果您对此有任何疑问,请阅读:Different between single and double quotes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多