简体   繁体   English

修复数组的索引超出范围?

[英]Fix Index out of bounds for array?

I am creating a maze game using console of eclipse.我正在使用 eclipse 控制台创建迷宫游戏。 I am trying to move a player, and save the players new position so I can move him in another direction.我正在尝试移动玩家,并为玩家保存新的 position,这样我就可以将他移动到另一个方向。 However, every time I try to move the player, I get Index ___ out of bounds for length 5 (usually -1 or 5).但是,每次我尝试移动播放器时,我都会得到索引 ___ 超出长度 5(通常为 -1 或 5)的范围。 It is skipping over the entire array, and I am unsure how to fix it.它正在跳过整个数组,我不确定如何修复它。

When ran, it prints the following运行时,它会打印以下内容

P. . P.. . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . T .吨。

. . . . ! ! . .

Please select one of the following:请 select 以下之一:

Press 1 to move up.按 1 向上移动。

Press 2 to move down.按 2 向下移动。

Press 3 to move left.按 3 向左移动。

Press 4 to move right.按 4 向右移动。

Press 0 to stop playing the game.按 0 停止游戏。

I only listed one movement, but the rest are the same, with interchanging variables.我只列出了一个机芯,但 rest 是相同的,具有互换变量。 Every option returns Index out of bounds.每个选项都返回索引超出范围。 And I was suggested to create a 'check' system but that doesn't solve the problem.有人建议我创建一个“检查”系统,但这并不能解决问题。 It just runs the program without allowing any movements, because every movement returns out of bounds.它只是运行程序而不允许任何动作,因为每个动作都会返回出界。 I believe it has something to do with the line我相信这与线路有关

mazeStructure[vxTemp][vyTemp] = mazeStructure[i][k-1];

Either that or something to do with the for conditions.无论是那个还是与 for 条件有关的东西。 If someone could provide a fix that would be greatly appreciated.如果有人可以提供修复,将不胜感激。

Scanner input = new Scanner(System.in);
int rowsTotal = 5;
int columnsTotal = 5;
int vxTemp = 0;
int vyTemp = 0;
String[][] mazeStructure =  {
        {"P",".",".","!","."},
        {".",".",".",".","."},
        {".",".",".","!","."},
        {"!","!",".","T","."},
        {"!",".","!","!","."},
};

 boolean won = false;
while (won == false){
    for (int a = 0; a < rowsTotal; a++){
        for (int b = 0; b < columnsTotal; b++){
            System.out.print(mazeStructure[a][b]);
            System.out.print(" ");
    }
        System.out.print("\n");
  }


    System.out.printf("\n");
    System.out.println("Please select one of the following:");
    System.out.println("Press 1 to move up.");
    System.out.println("Press 2 to move down.");
    System.out.println("Press 3 to move left.");
    System.out.println("Press 4 to move right.");
    System.out.println("Press 0 to stop playing the game.");
    int choice = input.nextInt();
    int a = 0;

   
    if (choice == 1 && a >= 0 && a < columnsTotal){
    for (int i = 0; i < rowsTotal; i++){
        for (int k = 0; k < columnsTotal; k++){
        if (mazeStructure[vxTemp][vyTemp-1].equals("!") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){
                    mazeStructure[vxTemp][vyTemp] = ".";
                    mazeStructure[vxTemp][vyTemp-1] = "P";
                    mazeStructure[vxTemp][vyTemp] = mazeStructure[i][k-1];
       }else if (mazeStructure[vxTemp][vyTemp-1] == "!"){
                    System.out.println("Move isn't allowed.");
       }else {
        continue;}

In the line if (mazeStructure[vxTemp][vyTemp-1].equals(".") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){ you try to access mazeStructure at [vxTemp][vyTemp-1] .if (mazeStructure[vxTemp][vyTemp-1].equals(".") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){您尝试访问mazeStructure[vxTemp][vyTemp-1] You initialize vyTemp at 0 and it doesnt change before this point.您将vyTemp初始化为 0,并且在此之前它不会改变。 You start that loop with trying vyTemp - 1 which will be -1.您通过尝试vyTemp - 1开始该循环,这将是 -1。 That will be out of bounds for your array.这将超出您的数组的范围。

You do that same with variable k within your loop.您对循环中的变量k执行相同的操作。 The first time around k will be zero. k附近的第一次将为零。 So all though k - 1 existing is ok, you can't try to access any array at some -1 index.因此,尽管存在k - 1是可以的,但您不能尝试访问某个 -1 索引处的任何数组。

I guess the problem is vyTemp-1 in this line我猜问题出在这一行的vyTemp-1

if (mazeStructure[vxTemp][vyTemp-1].equals(".") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){

The value of vyTemp is 0 initially vyTemp 的值最初为 0

Scanner input = new Scanner(System.in);
int rowsTotal = 5;
int columnsTotal = 5;
int vxTemp = 0;
int vyTemp = 0;
String[][] mazeStructure =  {
        {"P",".",".","!","."},
        {".",".",".",".","."},
        {".",".",".","!","."},
        {"!","!",".","T","."},
        {"!",".","!","!","."},
};

 boolean won = false;
while (won == false){
    for (int a = 0; a < rowsTotal; a++){
        for (int b = 0; b < columnsTotal; b++){
            System.out.print(mazeStructure[a][b]);
            System.out.print(" ");
    }
        System.out.print("\n");
  }


    System.out.printf("\n");
    System.out.println("Please select one of the following:");
    System.out.println("Press 1 to move up.");
    System.out.println("Press 2 to move down.");
    System.out.println("Press 3 to move left.");
    System.out.println("Press 4 to move right.");
    System.out.println("Press 0 to stop playing the game.");
    int choice = input.nextInt();
    int a = 0;

   
    if (choice == 1 && a >= 0 && a < columnsTotal){
    for (int i = 0; i < rowsTotal; i++){
        for (int k = 0; k < columnsTotal; k++){

//There error in if condition. //if条件有错误。 If your choice is 1. It will run this if condition 'mazeStructure[vxTemp][vyTemp-1].equals(".")'.如果您的选择是 1。如果条件 'mazeStructure[vxTemp][vyTemp-1].equals(".")',它将运行此命令。 As vyTemp = 0. vyTemp -1 < 0. Which will java.lang.ArrayIndexOutOfBoundsException由于 vyTemp = 0. vyTemp -1 < 0. 这将 java.lang.ArrayIndexOutOfBoundsException

 if (mazeStructure[vxTemp][vyTemp-1].equals("!") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){
                    mazeStructure[vxTemp][vyTemp] = ".";
                    mazeStructure[vxTemp][vyTemp-1] = "P";
                    mazeStructure[vxTemp][vyTemp] = mazeStructure[i][k-1];
       }else if (mazeStructure[vxTemp][vyTemp-1] == "!"){
                    System.out.println("Move isn't allowed.");
       }else {
        continue;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM