我正在尝试删除.txt文件中的每个X.选择坐标时,如果是X
,则应替换为0
,然后还应替换每个接触它的X
(对角线除外),直到不再有坐标为止。 X
可以触及。我目前的代码没有得到这些结果。
当我在java.lang.ArrayIndexOutOfBoundsException: 9
的{{1}}和0
getCoord(grid.length, "row")
输入2
时,我收到此错误getCoord(grid[0].length, "column")
。
谢谢你的帮助。
main()
00X000000
0XXXXXXX0
0X00000XX
0X0X000XX
0X00000X0
0XXXXXXX0
XXXX00XX0
000XX0000
答案 0 :(得分:0)
您需要在rubOut函数中添加边界检查:
public static void rubOut(String[][] g, int tRow, int tCol)
{
if((tRow < 0) || (tRow >= g.length))
return;
if((tCol < 0) || (tCol >= g[tRow].length))
return;
...
}
即使你的row = 0和column = 2的初始索引在边界内,当你调用rubOut(g, tRow - 1, tCol);
时你也会超出范围,因为tRow
将是-1。
答案 1 :(得分:0)
使用此for循环替换X over 0
public static void main(String [] args){
String[][] str ={{"0","0","X","0","0","0","0","0","0"},
{"0","X","X","X","X","X","X","X","0"},{"0","X","0","0","0","0","0","X","X"},
{"0","X","0","X","0","0","0","X","X"},{"0","X","0","0","0","0","0","X","0"},};
for (int i=0; i<str.length; i++) {
for(int j=0; j<str[i].length; j++){
System.out.print(str[i][j]);
}
System.out.println();
}
System.out.println();
for (int i=0; i<str.length; i++) {
for(int j=0; j<str[0].length; j++){
if(i==j){
str[i][j]="0";
}
System.out.print(str[i][j]);
}
System.out.println();
}
}
这将取代X超过0