【发布时间】:2022-11-16 21:41:52
【问题描述】:
我需要创建一个棋盘游戏和 output of the array needs to look like this:
But the output of my code looks like this:
我怎样才能摆脱这些标志并使其看起来像我需要的输出?
这是我的代码:
private char[][] boardMatrix;
public TryingSth() {
boardMatrix = new char[3][3];
// boardMatrix[0][0] = 'H';
for (int i = 0; i < boardMatrix.length; i++) {
for (int j = 0; j < boardMatrix.length; j++) {
if (i==0 && j==0){
System.out.print('H');
} else if (i ==2 && j==2){
System.out.print('G');
}else
boardMatrix[i][j] = '_';
System.out.print(boardMatrix[i][j] + " ");
}
System.out.println();
}
}
【问题讨论】:
-
第一步是编写可以编译的代码。除了您的方法缺少返回类型(以及命名约定的忽略)之外,您的代码完全按照它应该做的去做
-
但是为什么不将 G 和 H 存储在那个 boardMatrix 中呢?
标签: java arrays multidimensional-array