gpt4 book ai didi

java - 让字符串包含方法在我的代码中工作

转载 作者:行者123 更新时间:2023-12-02 02:31:46 25 4
gpt4 key购买 nike

我被要求开发一款让角色探索世界的游戏。世界是一个二维数组。玩家从阵列中的某个单元格开始,并通过到达阵列边缘的任何单元格进入下一个级别。在每个回合中,玩家可以向北、向南、向东或向西移动。然而,玩家无法移回到已经访问过的单元格。我必须为此使用字符串包含方法。我的代码不断给我一个数组越界错误字符串索引超出范围-1。我不知道它为什么这样做。我真的很感激一些帮助,使其发挥作用。太感谢了。此代码是使用 Netbeans 用 java 编写的。我需要我的字符串包含方法才能工作。

错误发生在这一行: 字符串 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: " );


}
}

}

最佳答案

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

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

因此在运行时导致StringIndexOutOfBoundsException

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

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

关于java - 让字符串包含方法在我的代码中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com