玩家在数组中的移动

我是初学者所以请原谅我。我正在尝试制作一个基于文本的游戏。我在让玩家穿过阵列时遇到问题。0 = 空位 p = 玩家

[ 0, 0, 0, 0, 0]

[ 0, 0, 0, 0, 0]

[ 0, 0, 0, 0, 0]

[ 0, 0, 0, 0, 0]

[ 0, 0, p, 0, 0]

我想要的结果是,当我输入 1 时,玩家向上移动

[ 0, 0, 0, 0, 0]

[ 0, 0, 0, 0, 0]

[ 0, 0, 0, 0, 0]

[ 0, 0, p, 0, 0]

[ 0, 0, 0, 0, 0]

但我认为我的代码显示它是

[ 0, 0, 0, 0, 0]

[ 0, 0, 0, 0, 0]

[ 0, 0, 0, 0, 0]

[ 0, 0, p, 0, 0]

[ 0, 0, p, 0, 0]

公共静态无效主(字符串[] args){

    //starting player position

    int px = 4;

    int py = 2;

    String player = "P";


    String map[][] = new String[5][5];


    for(int i = 0; i < map.length; i++) 

        for (int j = 0; j < map.length; j++) {


            map[px][py] = player;


            System.out.println(map[i][j]);


            int move;

            Scanner scanner = new Scanner(System.in);

            move = scanner.nextInt();


            //up

            if(move == 1) {

                map[px][py + 1] = player;

                printMap(map);

            }


            //right

            if(move == 2) {

                map[px + 1][py] = player;

                printMap(map);

            }


            //left

            if(move == 3) {

                map[px - 1][py] = player;

                printMap(map);

            }


             //down

            if(move == 4) {

                map[px][py - 1] = player;

                printMap(map);

            }


        }


}



public static void printMap(String a[][]) {


    for ( int i = 0; i < a.length; i++)

        for( int j = 0; j < a[i].length; j++) {


            System.out.println( a[i][j]);


        }


POPMUISE
浏览 60回答 1
1回答

慕丝7291255

对于您的问题,您只需将其设置回零,例如://upif(move == 1) {&nbsp;&nbsp; map[px][py + 1] = "0";&nbsp; map[px][py + 1] = player;&nbsp; printMap(map);}但是您的代码存在几个问题:如果你想向上移动,那么你应该从行索引中减去一个,其他方向也是如此(请参见下面的代码)。当你声明一个字符串数组时,它的内容将为空,你应该用零(字符串零)初始化它。看看我如何打印二维数组,我使用的每一行而print不是println.您还需要更新 px/ py 索引,如 @Nexevis 提到的请参阅此代码示例:import java.util.Scanner;public class Example{&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; //starting player position&nbsp; &nbsp; &nbsp; &nbsp; int px = 4;&nbsp; &nbsp; &nbsp; &nbsp; int py = 2;&nbsp; &nbsp; &nbsp; &nbsp; String player = "P";&nbsp; &nbsp; &nbsp; &nbsp; String map[][] = new String[5][5];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < map.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < map[0].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[i][j]="0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < map.length; i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < map.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[px][py] = player;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(map[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("1-move up \n2-move right \n3-move left\n4-move- down");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int move;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move = scanner.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //up&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(move == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[px][py] = "0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[--px][py] = player;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMap(map);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //right&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(move == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[px][py] = "0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[px][++py] = player;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMap(map);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(move == 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[px][py] = "0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[px ][--py] = player;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMap(map);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //down&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(move == 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[px][py] = "0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[++px][py] = player;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printMap(map);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void printMap(String[][] a) {&nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i < a.length; i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for( int j = 0; j < a[1].length; j++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print( a[i][j]+" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}请注意,您应该添加对数组索引的验证以防止 IndexOutOfBound 异常
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java