Java if-else语句在第三次迭代后似乎无法正常工作

Java if-else语句在第三次迭代后似乎无法正常工作,java,arrays,if-statement,Java,Arrays,If Statement,我在下面写了一个程序,用来打印从1到12的乘法表。我还想打印1至12下来的一方由几个空格和|分开。我设置了一个if-else语句来减少所需的空格数,具体取决于该数字中的位数,但当我运行下面的代码时,它会打印出前两行,然后停止 也许今天我盯着代码看了太久,但我一辈子都搞不懂它为什么会这样 顺便说一句,当我在末尾删除if-else语句并只使用System.out.printlnprint时,表本身可以很好地打印出来;在嵌套for循环之后 import java.util.*; public cl

我在下面写了一个程序,用来打印从1到12的乘法表。我还想打印1至12下来的一方由几个空格和|分开。我设置了一个if-else语句来减少所需的空格数,具体取决于该数字中的位数,但当我运行下面的代码时,它会打印出前两行,然后停止

也许今天我盯着代码看了太久,但我一辈子都搞不懂它为什么会这样

顺便说一句,当我在末尾删除if-else语句并只使用System.out.printlnprint时,表本身可以很好地打印出来;在嵌套for循环之后

import java.util.*;


public class MultiplicationTable{

  public static void main(String[] args){

    int n = 12;
    int temp;
    int length;

    String[][] table = new String[n][n];

    /**Assign values to the array*/
    /**These are the rows*/
    for(int i = 1; i <= n; i++){

        /**These are the columns*/
        for(int j = 1; j <= n; j++){

            /**this is the current multiplication value*/
            temp = i*j;
            /**assigning the value to it's place in the array*/
            table[i-1][j-1] = String.valueOf(temp);

            /**determining how many spaces are required to keep the table ordered*/
            length = String.valueOf(temp).length();                
            if(length==1){
                table[i-1][j-1] = table[i-1][j-1]+"   ";
            }
            else if(length==2){
                table[i-1][j-1] = table[i-1][j-1]+"  ";
            }
            else{
                table[i-1][j-1] = table[i-1][j-1]+" ";
            }
        }

    }

    /**This is to print out the array*/
    String print;

    for(int x = 0; x<n; x++){

        print = "";
        for(int y=0; y<n; y++){
            print = print + String.valueOf(table[x][y]);
        }

        /**This is for determining how many spaces are needed in front of the lines*/
        length = String.valueOf(x+1).length();

        //This is for error testing
        System.out.println("");
        System.out.println(length);
        System.out.println("");
        //End of error testing

        if(x==1){

            System.out.println(x+"  |"+print);
        }
        else if(x==2){
            System.out.println(x+" |"+print);
        }
    }

  }  

}

如果x为1,则打印一些内容;如果x是2,你打印的东西是一样的,但不管怎样;否则,你什么也不做。混乱在哪里?

我看不出这张桌子有什么意义。我会使用格式化输出和类似的东西

int n = 12;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        if (j != 0) {
            System.out.print(" | ");
        }
        System.out.printf("% 4d", (1+i) * (1+j));
    }
    System.out.println();
}

这个答案很差,主要是因为这个问题很差,但是答案很差。我不打算调试上面的代码来看看这个建议是否有效,但是你的ifx==1应该是iflength==1。请学习使用调试器逐步调试代码,以便您自己查看流程。啊,该死的。非常感谢。你当然是对的。
 1 |    2 |    3 |    4 |    5 |    6 |    7 |    8 |    9 |   10 |   11 |   12
 2 |    4 |    6 |    8 |   10 |   12 |   14 |   16 |   18 |   20 |   22 |   24
 3 |    6 |    9 |   12 |   15 |   18 |   21 |   24 |   27 |   30 |   33 |   36
 4 |    8 |   12 |   16 |   20 |   24 |   28 |   32 |   36 |   40 |   44 |   48
 5 |   10 |   15 |   20 |   25 |   30 |   35 |   40 |   45 |   50 |   55 |   60
 6 |   12 |   18 |   24 |   30 |   36 |   42 |   48 |   54 |   60 |   66 |   72
 7 |   14 |   21 |   28 |   35 |   42 |   49 |   56 |   63 |   70 |   77 |   84
 8 |   16 |   24 |   32 |   40 |   48 |   56 |   64 |   72 |   80 |   88 |   96
 9 |   18 |   27 |   36 |   45 |   54 |   63 |   72 |   81 |   90 |   99 |  108
10 |   20 |   30 |   40 |   50 |   60 |   70 |   80 |   90 |  100 |  110 |  120
11 |   22 |   33 |   44 |   55 |   66 |   77 |   88 |   99 |  110 |  121 |  132
12 |   24 |   36 |   48 |   60 |   72 |   84 |   96 |  108 |  120 |  132 |  144