Java 获取字符串包含在我的代码中工作的方法
我被要求开发一个游戏,其中包括角色探索世界。世界是一个二维阵列。玩家从阵列中的某个单元开始,通过到达阵列边缘的任何单元进入下一个级别。在每一个回合中,玩家可以向北、向南、向东或向西移动。但是,玩家不能移回已经访问过的单元格。我必须为此使用stringcontains方法。我的代码不断给我一个数组越界错误字符串索引超出范围-1。我不知道它为什么这样做。我真的很感激在这方面能得到一些帮助。非常感谢你。这段代码是使用Netbeans用java编写的。我需要我的字符串包含方法才能工作 错误发生在这一行: 字符串bestdirection=path.substring(0,commaIdx) 以下是全部代码:Java 获取字符串包含在我的代码中工作的方法,java,arrays,string,substring,Java,Arrays,String,Substring,我被要求开发一个游戏,其中包括角色探索世界。世界是一个二维阵列。玩家从阵列中的某个单元开始,通过到达阵列边缘的任何单元进入下一个级别。在每一个回合中,玩家可以向北、向南、向东或向西移动。但是,玩家不能移回已经访问过的单元格。我必须为此使用stringcontains方法。我的代码不断给我一个数组越界错误字符串索引超出范围-1。我不知道它为什么这样做。我真的很感激在这方面能得到一些帮助。非常感谢你。这段代码是使用Netbeans用java编写的。我需要我的字符串包含方法才能工作 错误发生在这一行:
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: " );
}
}
}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
//制作阵列
扫描仪=新的扫描仪(System.in);
System.out.print(“迷宫中有多少行?”);
int rows=scanner.nextInt();
System.out.print(“迷宫中有多少列?”);
int colums=scanner.nextInt();
int[][]迷宫=新的int[行][列];
对于(int i=0;i 对于(int j=0;j,这是因为字符串变量path
不包含逗号,
,它将返回-1
的索引:
int commaIdx = path.indexOf(",");
因此在运行时会导致StringIndexOutOfBoundsException
String bestdirection = path.substring(0,commaIdx);
首先检查并确保您的commaIdx
具有有效值。这是因为您的字符串变量path
不包含逗号,
,该变量将在以下位置返回-1
索引:
int commaIdx = path.indexOf(",");
因此在运行时会导致StringIndexOutOfBoundsException
String bestdirection = path.substring(0,commaIdx);
首先检查并确保您的commaIdx
具有有效值。我认为您在创建path变量时忘记添加“,”。
在字符串中添加逗号后,问题可能会得到解决,如下所示:
String path = (Integer.toString(x) + "," + Integer.toString(y));
我想您在创建path变量时忘了添加“,”。
在字符串中添加逗号后,问题可能会得到解决,如下所示:
String path = (Integer.toString(x) + "," + Integer.toString(y));
如果您希望人们尝试阅读您的代码,请适当缩进如果您希望人们尝试阅读您的代码,请适当缩进