Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改二维数组中显示的字符?_Java_Arrays_Multidimensional Array - Fatal编程技术网

Java 如何更改二维数组中显示的字符?

Java 如何更改二维数组中显示的字符?,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我正在用java创建这个控制台游戏,玩家将被提示输入棋盘的尺寸,并被要求使用WASD键在棋盘内移动,WASD键分别表示向上、向左、向下和向右。我已经展示了棋盘和带有@符号的玩家的初始位置。每当我想让玩家在棋盘上移动时,我都很难改变符号。这是我的密码 public class Bord { public int n; public int i, j, a, b, c; public char move; public char board[][] = new cha

我正在用java创建这个控制台游戏,玩家将被提示输入棋盘的尺寸,并被要求使用WASD键在棋盘内移动,WASD键分别表示向上、向左、向下和向右。我已经展示了棋盘和带有@符号的玩家的初始位置。每当我想让玩家在棋盘上移动时,我都很难改变符号。这是我的密码

public class Bord {
    public int n;
    public int i, j, a, b, c;
    public char move;
    public char board[][] = new char[n][n];

    Scanner in = new Scanner(System.in);

    public Bord() {
        System.out.println("\n\t************");
        System.out.println("\n\tThe Ant Game\n");
        System.out.println("\t************");
        System.out.println("\nInstructions: \nWelcome to Ant Game. You are the ant (@). \nUse 'w', 'a', 's', or 'd' to move");
        System.out.print("up, left, down or right consecutively. \nThere will be traps or rewards hidden \nin each tile (*) so be careful. Good luck!");
        System.out.print("\n\nEnter board dimension: ");
        n = in.nextInt();
        System.out.println();

        char board[][] = new char[n][n];

        for(i=0; i<n; i++){
            for(j=0; j<n; j++)
                board[i][j] = '*'; 
        }             

        i = 0;
        j = 0;
        board[i][j] = '@';

        for(a=0; a<n; a++){
            System.out.print("\t");
            for(b=0; b<n; b++){
                System.out.print(board[a][b]);
                for(c=0; c<n; c++)
                    System.out.print("");
                }
                System.out.println();
            }
             System.out.println();


        System.out.print("Press WASD for movement: ");
        move = in.next().charAt(0);


        switch (move)
        {
            //Move Up   
            case 'w':{
                if(i==0&&j==0){
                    System.out.println("End of boundary.");
                    System.out.print("Move again: ");
                    move = in.next().charAt(0);
                }
                else{
                    for(i=0; i<n; i++){
                        for(j=0; j<n; j++)
                            board[i][j] = '*'; 
                }             

                    i = 0;
                    j = 0;
                    board[i-1][j] = '@';

                    display();

                System.out.print("Move again: ");
                move = in.next().charAt(0);
                }
            break;    
            }

            //Move Left
            case 'a':
                System.out.println("End of boundary.");
                System.out.print("Move again: ");
                move = in.next().charAt(0);
             break;   

             //Move Right
            case 'd':{
                board[i][j+1] = '@';

                i = 0;
                j = 0;
                board[i][j] = '*';

                System.out.println();
                for(a=0; a<n; a++){
                    System.out.print("\t");
                    for(b=0; b<n; b++){
                        System.out.print(board[a][b]);
                        for(c=0; c<n; c++)
                            System.out.print("");
                        }
                        System.out.println();
                    }
                     System.out.println();  

                 System.out.print("Move again: ");
                 move = in.next().charAt(0);     
            break;          
            }

            //Move Down
            case 's':{
                board[i+1][j] = '@';

                i = 0;
                j = 0;
                board[i][j] = '*';

                System.out.println();
                for(a=0; a<n; a++){
                    System.out.print("\t");
                    for(b=0; b<n; b++){
                        System.out.print(board[a][b]);
                        for(c=0; c<n; c++)
                            System.out.print("");
                        }
                        System.out.println();
                    }
                     System.out.println();  

                 System.out.print("Move again: ");
                 move = in.next().charAt(0);     
            break;          
            }

            default:{ 
                System.out.print("Incorrect move. Press WASD only: ");
                move = in.next().charAt(0);
            }

        }

    System.out.print("Move again.");
    move = in.next().charAt(0);

  }  

  public void display(){

        for(a=0; a<n; a++){
            System.out.print("\t");
                for(b=0; b<n; b++){
                 System.out.print(board[a][b]);
                    for(c=0; c<n; c++)
                       System.out.print("");
                     }
                     System.out.println();
                 }
                 System.out.println();
    }


        public static void main(String[] args){
            Bord brd = new Bord();
        }


}
这是我未完成的代码。由于这是一个动态2D数组,如何在用户每次移动到他/她的选择方向时将符号从*更改为@?

访问数组中的第n个元素是一种越界访问


第一个元素位于索引0处,第二个元素位于1处,以此类推。最后一个元素位于索引n-1处。

在java中,在大多数语言中,数组的索引都以零开始。因此,第一个元素位于索引0处,以此类推

此外,改进代码的一种方法是用一个嵌套循环替换前两个循环

for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        board[i][j] = '*';
    }
}

看看你的代码。在循环aShould board[a][n]='*'中使用了正确的边界;被移除?。。考虑一下数组的边界。如果你知道你为什么放Ok,我就试试这个。非常感谢你!我感谢所有能得到的帮助