乘法数组table

Multiplication Array table

我需要得到这个结果: input I wanted to have

我已经很接近了,但最后一步要了我的命:

public static void printMultiplicationTable(int size) {
    System.out.print("\t");
    for (int i = 0; i <= size; i++) {
        for (int j = 0; j <= size; j++) {
            if (j == 0) System.out.print( i );
            else if (i == 0) System.out.print("\t" + j);
            else System.out.print("\t" + i * j);
        }
        System.out.println();
    }
}

如何在第一行取空 space 并从 1 而不是 0 开始?

你们确实很亲密。您的第一个 System.out.print("\t"); 导致了额外的制表符。此外,我还添加了条件 i==0 && j==0 时的输出。下面给出的是打印所需结果的程序:

public class TestMyClass {
    public static void main(String[] args) {
        printMultiplicationTable(4);
    }

    public static void printMultiplicationTable(int size) {
        for (int i = 0; i <= size; i++) {
            for (int j = 0; j <= size; j++) {
                if (i==0 && j==0)
                    System.out.print("");
                else if (j == 0)
                    System.out.print(i);
                else if (i == 0)
                    System.out.print("\t" + j);
                else
                    System.out.print("\t" + i * j);
            }
            System.out.println();
        }
    }
}

输出:

    1   2   3   4
1   1   2   3   4
2   2   4   6   8
3   3   6   9   12
4   4   8   12  16

你的"one loop to rule them all"让你更难做到这一点。

将其分解为更简单的概念。

  1. 先打印 header 行。
  2. 打印行并检查第一个索引以打印 left-side
  3. Pre-calculate最大值的宽度(用于格式化)
public class MathTutor {
    public static void main(String[] args) {
        printMultiplicationTable(3);
        printMultiplicationTable(6);
        printMultiplicationTable(12);
    }

    public static void printMultiplicationTable(int size) {
        String numberFormat = String.format("%%%dd", maxDigits(size));
        for (int col = 0; col < size; col++) {
            System.out.print("\t");
            System.out.printf(numberFormat, col + 1);
        }
        System.out.println();
        for (int row = 1; row <= size; row++) {
            System.out.printf(numberFormat, row);
            for (int col = 1; col <= size; col++) {
                System.out.print("\t");
                System.out.printf(numberFormat, row * col);
            }
            System.out.println();
        }
    }

    public static int maxDigits(int size) {
        return (int) Math.floor(Math.log10(Math.pow(size, 2))) + 1;
    }
}

乘法 table 为 3

    1   2   3
1   1   2   3
2   2   4   6
3   3   6   9

乘法 table 为 6

     1   2   3   4   5   6
 1   1   2   3   4   5   6
 2   2   4   6   8  10  12
 3   3   6   9  12  15  18
 4   4   8  12  16  20  24
 5   5  10  15  20  25  30
 6   6  12  18  24  30  36

12 的乘法 table

      1   2   3   4   5   6   7   8   9  10  11  12
  1   1   2   3   4   5   6   7   8   9  10  11  12
  2   2   4   6   8  10  12  14  16  18  20  22  24
  3   3   6   9  12  15  18  21  24  27  30  33  36
  4   4   8  12  16  20  24  28  32  36  40  44  48
  5   5  10  15  20  25  30  35  40  45  50  55  60
  6   6  12  18  24  30  36  42  48  54  60  66  72
  7   7  14  21  28  35  42  49  56  63  70  77  84
  8   8  16  24  32  40  48  56  64  72  80  88  96
  9   9  18  27  36  45  54  63  72  81  90  99 108
 10  10  20  30  40  50  60  70  80  90 100 110 120
 11  11  22  33  44  55  66  77  88  99 110 121 132
 12  12  24  36  48  60  72  84  96 108 120 132 144