反转递增三角形图案

Invert incrementing triangle pattern

我正在编写一些代码来创建一个递增数字直角三角形,看起来像这样:

           1
         1 2
       1 2 3
     1 2 3 4
   1 2 3 4 5
 1 2 3 4 5 6

我不确定如何让我的代码输出一个三角形到那个形状,因为我的代码输出相同的东西,除了倒置。

这是我的代码:

public class Main {
    public static void main(String[] args) {
        int rows = 6;
        for (int i = 1; i <= rows; ++i) {
            for (int j = 1; j <= i; ++j) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

我的猜测是,我不会递增某些值,而是递减它们,但是代码会 运行 无限的垃圾值,而不是我想要的。

您必须在打印数字之前打印 spaces 以使三角形看起来倒置,spaces 的数量取决于您跳过的数字数量,即 rows-i ,因此您可以从 i 循环到 rows 并在每次迭代中打印 space。

int rows = 6;
for (int i = 1; i <= rows; ++i) {
    for (int j = i; j < rows; j++) {
        System.out.print("  ");
    }
    for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
    }
    System.out.println();
}

输出:

          1 
        1 2 
      1 2 3 
    1 2 3 4 
  1 2 3 4 5 
1 2 3 4 5 6 

在打印每行数字之前需要打印一些空格,空格的数量应该根据行数递减:

int rows = 6;

for (int i = 1; i <= rows; ++i) {
    for (int j = rows - i; j >= 1; j--) {
        System.out.print("  ");
    }
    for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
    }
    System.out.println();
}

此前缀可以使用 String::join + Collections::nCopies:

构建
System.out.print(String.join("", Collections.nCopies(rows - i, "  ")));

或者自 Java 11 起,可以使用 String::repeat:

替换此前缀
System.out.print("  ".repeat(rows - i));

可以使用属性,对于包含i的行作为行的最大行数,空格数可以计算为2*(rows-i)。您可以像下面这样重写您的程序:

public class Main {
    public static void main(String[] args) {
        int rows = 6;
        for (int i = 1; i <= rows; ++i) {
            for (int nspaces = 0; nspaces < 2 * (rows - i); ++nspaces) {
                System.out.print(" ");
            }
            for (int j = i; j > 0; --j) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

输出:

          1 
        2 1 
      3 2 1 
    4 3 2 1 
  5 4 3 2 1 
6 5 4 3 2 1 

您可以使用两个嵌套的 for 循环打印一个 倒三角形 ,如下所示:

// the number of rows and the
// number of elements in each row 
int n = 6;
// iterating over rows with elements
for (int i = 0; i < n; i++) {
    // iterating over elements in a row
    for (int j = 0; j < n; j++) {
        // element
        if (i + j >= n - 1) {
            // print an element
            System.out.print(i + j + 2 - n);
        } else {
            // print a whitespace
            System.out.print(" ");
        }
        // suffix
        if (j < n - 1) {
            // print a delimiter
            System.out.print(" ");
        } else {
            // print a new line
            System.out.println();
        }
    }
}

输出:

          1
        1 2
      1 2 3
    1 2 3 4
  1 2 3 4 5
1 2 3 4 5 6

另请参阅:

您可以使用带有两个递增变量的单个 while 循环,而不是两个嵌套的 for 循环。迭代次数保持不变。

int n = 7, i = 0, j = 0;
while (i < n) {
    // element
    if (i + j >= n - 1) {
        // print an element
        System.out.print(i + j + 2 - n);
    } else {
        // print a whitespace
        System.out.print(" ");
    }
    // suffix
    if (j < n - 1) {
        // print a delimiter
        System.out.print(" ");
        j++;
    } else {
        // print a new line
        System.out.println();
        j = 0;
        i++;
    }
}

输出:

            1
          1 2
        1 2 3
      1 2 3 4
    1 2 3 4 5
  1 2 3 4 5 6
1 2 3 4 5 6 7

另请参阅:Optimized Bubble Sort

你的方法几乎是正确的——使用两个嵌套的for循环,剩下的就是添加一个if else语句并计算sum 坐标 ij

Try it online!

public static void main(String[] args) {
    int n = 6;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            System.out.print(" ");
            int sum = i + j;
            if (sum > n)
                System.out.print(sum - n);
            else
                System.out.print(" ");
        }
        System.out.println();
    }
}

输出:

           1
         1 2
       1 2 3
     1 2 3 4
   1 2 3 4 5
 1 2 3 4 5 6

另请参阅: