简体   繁体   English

尝试在Java中打印Chars的2D数组

[英]Trying to print a 2D array of Chars in Java

My program doesn't print anything at all. 我的程序根本不打印任何东西。

First I initialized the board in a seperate class (board): 首先,我在一个单独的类(板)中初始化了电路板:

       public class Board {
          public char board[][] = new char[9][9];   
          public void main(char[] args){

        for(int i=0; i<9; i++){
            board[i][0] = '_';
            board[i][8] = '_';
        }
        for(int h=0; h<9; h++){
            board[0][h] = '|';
            board[8][h] = '|';
        }

        for(int x=0; x>9; x++){
            for(int y=0; y>9; y++){
                System.out.println(board[x][y]);    
            }
        }
    }
}

Then called it in the main, with a PrintLine of "Hello World" to check that the code was being accessed. 然后在main中调用它,使用PrintLine “Hello World”来检查代码是否被访问。 No errors are flagged up but neither does it print anything at all. 没有错误被标记,但它也没有打印任何东西。 The Main is below also just to check that i havent done anything simple and stupid: 主要是下面也只是为了检查我没有做任何简单和愚蠢的事情:

    public static void main(String[] args) {  
    Ticker T = new Ticker();
    Board B = new Board();       
    for(int x=0; x>9; x++){
        for(int y=0; y>9; y++){
            System.out.println("Hello World");
            System.out.print(B.board[x][y]);

The terminating conditions on the for loops are incorrect. for循环的终止条件不正确。 Should be < , not > . 应该是< ,而不是> Change to: 改成:

for(int x=0; x<9; x++){
    for(int y=0; y<9; y++){

There is problem in your condition of for loop: - 你的循环条件有问题: -

for(int x=0; x>9; x++){
        for(int y=0; y>9; y++){

The code inside above loop never gets executed. 上面循环中的代码永远不会被执行。 It should be: - 它应该是: -

for(int x=0; x<9; x++){
        for(int y=0; y<9; y++){

Besides the incorrect condition in the for loops you should consider using 除了for循环中的错误条件,你应该考虑使用

public class Board {
    public char board[][] = new char[9][9];

    // this is the constructor, it will be called if you say "new Board()"
    // the "main" method you had here will not be called automatically
    public Board() {
        for (int i = 0; i < 9; i++) {
            board[i][0] = '_';
            board[i][8] = '_';
        }
        for (int h = 0; h < 9; h++) {
            board[0][h] = '|';
            board[8][h] = '|';
        }

        for (int x = 0; x < 9; x++) {
            for (int y = 0; y < 9; y++) {
                // just a print so it does not make new lines for every char
                System.out.print(board[x][y]);
            }
            // new line once one column (board[x][0] - board[x][8]) is printed
            // note: you proably want to turn around the x and y above since
            // I guess you want to print rows instead of columns
            System.out.println();
        }
    }
}

It fixes some problems 它修复了一些问题

  • the condition of the for loop that could never be true for循环的条件永远不会成立
  • replaced the main method with a constructor so the code you wrote there is executed 用构造函数替换main方法,以便执行您在那里编写的代码
  • changed to print the stuff that is printed in the inner loop in one line, so it looks like a board 更改为在一行中打印内部循环中打印的内容,因此它看起来像一块板

Now if you do 如果你这样做了

public static void main(String[] args) {  
    Ticker T = new Ticker();
    Board B = new Board(); // << this line triggers printing
    // ...
}

you should see some board like thing 你应该看到一些董事会喜欢的东西

have a look at for(int x=0; x>9; x++){ 看看for(int x=0; x>9; x++){

It should be for(int x=0; x<9; x++){ 它应该是for(int x=0; x<9; x++){

You might want to go for x<9 and y<9 and not > ! 你可能想要x <9和y <9而不是>! ;-) This is the condition loop. ;-)这是条件循环。 If it is false, the loop exit. 如果为false,则循环退出。 It is always false in your case. 在你的情况下,它总是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM