【问题标题】:if-else statement does not seem to work correctly after 3rd iterationif-else 语句在第 3 次迭代后似乎无法正常工作
【发布时间】:2014-11-05 01:37:59
【问题描述】:

我在下面编写了这个程序,用于打印从 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);
        }
    }

  }  

}

【问题讨论】:

  • 我不会调试上面的代码来看看这个建议是否有效,但你的if(x==1)应该是if(length==1)。请学习使用调试器来单步调试您的代码,以便您自己查看流程。
  • 啊该死的。谢谢你。你当然是对的。

标签: java arrays if-statement


【解决方案1】:

我看不出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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这个答案很差主要是因为问题很差,但答案仍然很差。
    猜你喜欢
    • 2013-11-23
    • 1970-01-01
    • 2016-06-18
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    相关资源
    最近更新 更多