【问题标题】:Invert incrementing triangle pattern反转递增三角形图案
【发布时间】:2021-06-18 23:53:39
【问题描述】:

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

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

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

【问题讨论】:

    标签: java loops for-loop ascii-art


    【解决方案1】:

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

    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
    

    另见:Printing a squares triangle. How to mirror numbers?

    【讨论】:

      【解决方案2】:

      您可以使用属性,对于包含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 
      

      【讨论】:

        【解决方案3】:

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

        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 
        

        【讨论】:

          【解决方案4】:

          您可以使用带有两个递增变量的单个 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

          【讨论】:

            【解决方案5】:

            您可以使用两个嵌套的 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
            

            另见:How to draw a staircase with Java?

            【讨论】:

              【解决方案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));
              

              【讨论】:

                猜你喜欢
                • 2018-01-05
                • 2014-03-11
                • 1970-01-01
                • 2016-12-02
                • 2014-12-08
                • 1970-01-01
                • 2011-03-29
                • 1970-01-01
                • 2022-01-03
                相关资源
                最近更新 更多