TicTacToe印刷板

时间:2015-07-15 21:46:52

标签: java

我正在尝试创建一个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] + " ");
            }
        }
    }



}

3 个答案:

答案 0 :(得分:2)

修改您的代码,如下所示:

library(ggplot2)

my_dat$O_with_labels <- 
  factor(my_dat[, 3], labels=paste('Vasc Path O:', levels(my_dat[, 3])))

ggplot(my_dat, 
       aes(x=Vascular_Pathology_M, y=Vascular_Pathology_F)) + 
  geom_jitter(aes(size=Output, color=Vascular_Pathology_F)) + 
  facet_wrap(~O_with_labels) +
  theme_bw() + 
  theme(axis.text.x = element_text(angle=45, hjust=1))

这样,您将在一行结束后移动到新行。

<强> __ UPDATE __

另一种方法是:

    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();
    }

答案 1 :(得分:1)

你可以这样做:

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(); // add this
}

此外,手动填充数组可能会更快(稍微),而不是使用for循环。 e.g

char[][] theBoard = new char[][]{{'_', '_', '_'}, {'_', '_', '_'}, {'_', '_', '_'}};

当一个简单的陈述可以做时,不需要复杂的逻辑! :)

答案 2 :(得分:0)

我相信它的

if ((j + 3) % 3 == 0)

的System.out.println()