获取字符串包含在我的代码中工作的方法

时间:2017-10-27 23:21:59

标签: java arrays string substring

我被要求开发一个涉及角色探索世界的游戏。世界是一个二维阵列。播放器在阵列中的某个单元格中启动,并通过到达阵列边缘上的任何单元格进入下一级别。每回合,玩家可以向北,向南,向东或向西移动。但是,玩家无法移回已经访问过的小区。我必须使用字符串contains方法。我的代码继续给我一个超出范围的数组错误字符串索引超出范围-1。我不知道为什么会这样做。我真的很感激这方面的一些帮助,使它工作。非常感谢。此代码使用Netbeans在java中编写。我需要我的字符串包含方法才能工作。

此行发生错误:              String bestdirection = path.substring(0,commaIdx);

以下是整个代码:

 public static void main(String[] args) {
    // TODO code application logic here
    //making array 
 Scanner scanner = new Scanner(System.in);
    System.out.print("How many rows are in the maze? ");
int rows = scanner.nextInt();
    System.out.print("How many colums are in the maze? ");
int colums = scanner.nextInt();
int [][] maze = new int [rows][colums];
for (int i = 0; i< maze.length; i++){
    System.out.print("Enter the danger in row " + (i+1) + ", separated by spaces: ");
    for (int j=0; j<maze[i].length; j++){



    maze[i][j] = scanner.nextInt();


}

}
           System.out.print("Enter the starting x coordinate: ");
    int x = scanner.nextInt();
    System.out.print("Enter the starting y coordinate: ");
    int y = scanner.nextInt();
    System.out.println("Moving to " + x + "," + y + "(danger level " + maze[x][y] + ")");
    String path = (Integer.toString(x) + Integer. toString(y));
    for ( int i=0; i<maze.length; i++){
        for( int j=0; j<maze[i].length; j++){
            if (x == i && y == j){
                System.out.print("*");

            }
            else{ 
                System.out.print(maze[i][j] + " ");
            }


        }
        System.out.println("");
    }

    while (x !=0 && y!=0 && x !=(rows -1) && y != (colums -1)){
    int left = maze[x][y-1];
    int right = maze[x][y+1];
    int up = maze[x-1][y];
    int down = maze[x+1][y];
    int commaIdx = path.indexOf(",");
    String bestdirection = path.substring(0,commaIdx);
   String bestdirection2 = path.substring(commaIdx);

   // if ( x == x && y == y+1){
      // left=1000;
  //  }
    //int count=0;
    if (left < right && left < up && left< down && !path.contains(path)){
        y = y-1;
         System.out.println("Moving to " + x+ "," + y + " (danger level " + maze[x][y] + ")" );
        for (int i =0; i<maze.length; i++ ){
            for (int j=0; j<maze[i].length; j++){

            if (x ==i && y==j){
                System.out.print("*");

            }
            else {
                System.out.print(maze[i][j] + " ");
            }
                System.out.println("");
            }

       // System.out.println("Moving to " + x + y + " (danger level " + maze[x][y] + ")" );
       // count++;

    }
       // if (x ==x && y ==(y-1)){
           // right =1000;
       // }

     if(right < left && right < up && right < down && !path.contains(path)){
        y= y+1;



        }
   // if (x == (x-1) && y==y){
        //up= 1000;
   // }
     if (up < right && up < left && up < down && !path.contains(path)){
        x = x-1;


    } 
    //if (x == (x+1) && y==y){
      //  down = 1000;
   // }
     if (down < right && down < left && down < up && !path.contains(path)){
        x = x+1;


    }
    }
    }


   // int total = maze[x][y] + maze[x][y];

    if (x ==0 || y ==0 || x == (rows -1) || y== (colums -1)){

        for (int i =0; i< maze.length; i++ ){
            for (int j=0; j<maze[i].length; j++){
                if (x ==i && y==j){
                    System.out.println("*");
                }
                else{
                    System.out.println(maze[i][j] + " ");
                }
            }
            System.out.println("");
        }
        System.out.println("Exited the world at: " + x + "," + y + " total danger faced: " );


        }
    }

    }

2 个答案:

答案 0 :(得分:1)

这是因为您的字符串变量path不包含逗号,,它会返回-1的索引:

int commaIdx = path.indexOf(",");

因此在您运行时导致StringIndexOutOfBoundsException

String bestdirection = path.substring(0,commaIdx);

检查并确保您的commaIdx具有有效值。

答案 1 :(得分:1)

我认为您在创建路径变量时忘记添加“,”。 在字符串中添加逗号后,可能会解决以下问题:

String path = (Integer.toString(x) + "," + Integer.toString(y));