if-else语句在第三次迭代后似乎不能正常工作



我在下面写了一个程序,用来打印从1到12的乘法表。我还想在被几个空格和"|"分隔的一侧打印1到12。我设置了一个if-else语句,根据数字中的数字来减少所需的空格数,但当我运行下面的代码时,它会打印出前两行,然后停止。

也许我今天盯着代码看的时间太长了,但我一辈子都想不出它为什么会这样。

顺便说一句,当我删除末尾的if-else语句,并且在嵌套的for循环之后只有"System.out.println(print);"时,表本身打印得很好。

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

我看不出table的意义。我会使用格式化的输出和类似的东西

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

输出为(按要求)

 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

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

最新更新