【发布时间】:2020-05-31 09:05:26
【问题描述】:
我正在编写游戏代码。该游戏的棋盘尺寸为 10x10。一开始,我把这个板子写成一个二维整数数组。现在我必须用字母替换黑板上的一些地方。它会根据玩家输入的坐标随字母变化。
private static int[][] board = new int[10][10]; //board
StringBuilder sb = new StringBuilder();
for (int[] array : board) {
sb.append('\n').append(Arrays.toString(array));
}
String new = sb.toString(); //I tried to convert this way
if (new[x][y].equals("r")){
new[x][y] = "d"; //I'm getting an error in this part.
}
【问题讨论】:
-
为什么不从一开始就使用字符串数组呢?所以在第一行你会有 private static String[][] board = new String[10][10];然后你必须相应地改变其他类型的方法
-
“新”字符串变量不是二维数组。所以我们不能像使用 [x][y] 改变整数二维数组一样改变值。
-
不能使用java关键字作为变量,为什么在数组中存储数值时要检查alpha?
-
@anon 尝试使用 2D String 数组复制 2D int 数组板数据。如我的回答所示。