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

时间:2014-11-05 01:38:00

标签: java arrays if-statement

我在下面有这个程序,我已经编写了从1到12打印出乘法表。我也希望在一些空格和一个“|”之间打印1到12。我有一个if-else语句设置,以根据该数字中的位数减少所需的空格数,但是当我运行下面的代码时,它打印出前两行,然后停止。

也许我今天已经盯着代码太久了,但是我不能为我的生活找出它为什么会这样做。

顺便说一句,当我在结尾处删除if-else语句并且只有“System.out.println(print);”时,表本身就打印出来了。在嵌套的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);
        }
    }

  }  

}

2 个答案:

答案 0 :(得分:0)

我没有看到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

答案 1 :(得分:-1)

如果x为1,则打印一些东西;如果x是2,你打印一些东西(同样的东西,但无论如何);否则,你什么都不做。困惑在哪里?