Fillgrid方法在井字游戏程序中不起作用

问题描述 投票:0回答:2

我正在为我的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
java arrays tic-tac-toe
2个回答
1
投票

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


1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.