我正在开发一款井字游戏。我编写了一个代码来打印一 block 板,它用下划线括号表示二维数组的空白空间。有谁知道我不能在最后一行打印下划线花括号的方法吗? Xs 和 Os 存储为字符串“X”和“O”谢谢!
public void PrintBoard()
{
System.out.println();
for (int i = 0; i < board.length; i++)
{
for (int j = 0; j < board.length; j++)
{
if (board[i][j] == null)
System.out.print("___");
else
System.out.print(board[i][j]);
if (j < 2)
System.out.print("|");
else
System.out.println();
}
}
System.out.println();
}
最佳答案
在打印下划线之前添加“if (i<2)”怎么样?
for (int i = 0; i < board.length; i++)
{
for (int j = 0; j < board[i].length; j++)
{
if (board[i][j] == null)
{
if (i < 2)
System.out.print("___");
}
else
System.out.print(board[i][j]);
if (j < 2)
System.out.print("|");
else
System.out.println();
}
}
System.out.println();
另外,我不喜欢你使用 board.length 作为内循环;它应该是 board[i].length。
关于java - 井字棋板二维阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20388027/