2017-10-27 9 views
0

안녕하세요. 저는 모두 초보 프로그래머입니다. 이것은 또한 내가이 웹 사이트에 게시 된 모든 두 번째 시간이므로 서식이 꺼져 있거나 사이트의 다른 질문과 유사하다면 사과드립니다. 이 코드에서는 세계를 탐험하는 캐릭터와 관련된 게임을 개발하라는 요청을 받았습니다. 세계는 2D 배열입니다. 플레이어는 배열의 일부 셀에서 시작하여 배열 가장자리의 셀에 도달하여 다음 수준으로 진행합니다. 각 턴마다 플레이어는 북쪽, 남쪽, 동쪽 또는 서쪽으로 이동할 수 있습니다. 그러나 플레이어는 이미 방문한 셀로 다시 이동할 수 없습니다. 나는 이것을 위해 string 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: "); 


     } 
    } 

    } 
+1

사람들이 코드를 읽으려면 코드를 올바르게 들여 쓰기를 원한다면 – khelwood

답변

0

당신의 문자열 변수 path가에 -1 당신에게 인덱스를 반환하는 쉼표 , 포함되어 있지 않기 때문에이다 : 따라서 원인

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

을 여기

전체 코드입니다 StringIndexOutOfBoundsException 달릴 때

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

먼저 commaIdx에 유효한 값이 있는지 확인하고 확인하십시오.