gpt4 book ai didi

java - TicTacToe 打印棋盘

转载 作者:行者123 更新时间:2023-11-29 05:02:58 43 4
gpt4 key购买 nike

我正在尝试创建 TicTacToe 游戏,但想问一个问题。我有一个 2d char 数组,现在用下划线填充 - '_'。我的问题是如何每行输出三个下划线?提前致谢!

import java.util.Scanner;
import java.util.*;
public class TicTacToe {

public static void main(String[] args){
Scanner kbd = new Scanner(System.in);

char[][] theBoard = new char[3][3];

for(int i = 0; i < theBoard.length; i++){
for(int j = 0; j < theBoard[i].length; j++){
theBoard[i][j] = '_';
}
}

for(int i = 0; i < theBoard.length; i++){
for(int j = 0; j < theBoard[i].length; j++){

System.out.print(theBoard[i][j] + " ");
}
}
}



}

最佳答案

修改你的代码如下:

    for(int i = 0; i < theBoard.length; i++){
for(int j = 0; j < theBoard[i].length; j++){

System.out.print(theBoard[i][j] + " ");
}
System.out.println();
}

这样,您将在一行完成后移至新行。

__更新__

另一种方法是:

    for(int i = 0; i < theBoard.length; i++){
StringBuffer buf = new StringBuffer();
for(int j = 0; j < theBoard[i].length; j++){
buf.append(theBoard[i][j] + " ");
}
System.out.println(buff.toString());
}

关于java - TicTacToe 打印棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441733/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com