【问题标题】:How do I write a program to output a triangle of numbers in Java?如何编写程序以在 Java 中输出三角形的数字?
【发布时间】:2011-11-09 19:17:50
【问题描述】:

我需要帮助。我的任务是编写一个使用嵌套循环的 Java 程序来打印出以下输出模式:

                   1 
                 1 2 1 
                1 2 4 2 1 
              1 2 4 8 4 2 1 
            1 2 4 8 16 8 4 2 1 
          1 2 4 8 16 32 16 8 4 2 1 
       1 2 4 8 16 32 64 32 16 8 4 2 1 
    1 2 4 8 16 32 64 128 64 32 16 8 4 2 1 

//pattern1
for(int outer=1;outer<=6;outer++) // outer loop controls number of rows
{
    for(int inner=1;inner<=outer; inner++) // another loop to control number of numbers in each row.
    {
        System.out.print(inner);
    }
    System.out.println(); // move the cursor from the end of the current line to the beggiing to the next line
}

//pattern 2
for(int outer =1; outer<=6 ; outer++) //outer loop controls number of rows
{
    //3-1 create spaces before numbers.
    for(int space=1; space<=6-outer; space++ ) //group controls number of spaces
    {
        System.out.print(" ");
    }

    //3-2 print out real numbers.
    for(int inner=1;inner<=outer; inner++) // another loop to control number of numbers in each row.
    {
        System.out.print(inner);
    }
    System.out.println();
}

这两个代码是背靠背的,但我不明白如何让数字2 4 8 16 等出现,并将它们背靠背。

我的代码有什么问题?在 Java 中有没有更好的方法来做到这一点?

【问题讨论】:

  • 我喜欢它的保护方式,但有一个负面的投票数。

标签: java loops


【解决方案1】:

具有位移和静态列大小/填充的简单版本 - 可以通过使用 Math.getExponent() 来改进动态重复空格和格式 %3d ...

public static void f(int n) {
    for (int i = 0; i < n; i++) {
        for (int l = n - i; l > 0; l--) { // padding for symmetry
            System.out.print("    "); 
        }
        for (int j = 0; j <= i; j++) { // "left side" of pyramid
            System.out.printf("%3d ", 1 << j); 
        }
        for (int k = i - 1; k >= 0; k--) { // "right side" of pyramid
            System.out.printf("%3d ", 1 << k); 
        }
        System.out.println();
    }
}

输出:

                              1 
                          1   2   1 
                      1   2   4   2   1 
                  1   2   4   8   4   2   1 
              1   2   4   8  16   8   4   2   1 
          1   2   4   8  16  32  16   8   4   2   1 
      1   2   4   8  16  32  64  32  16   8   4   2   1 
  1   2   4   8  16  32  64 128  64  32  16   8   4   2   1 

【讨论】:

    【解决方案2】:

    您将使用带有 if 语句控制输出的嵌套循环。

    此代码可以帮助您进行格式设置。您必须弄清楚如何添加 ||以便它翻转三角形以及如何格式化您的打印​​语句,使其看起来像那样。

    int totalWidth = 8;
    for (int row = 1; row <= totalWidth; row++) {
        for (int col = 1; col <= totalWidth; col++) {
          if (col <= totalWidth - row) {
            System.out.print(" ");
          }else {
            System.out.print("*");
          }
        }
        System.out.println();
      }
    

    会输出

           *
          **
         ***
        ****
       *****
      ******
     *******
    ********
    

    【讨论】:

      【解决方案3】:
      public class pyramid
      
          public static void f(int n) {
          for (int i = 0; i < n; i++) {
              for (int l = n - i; l > 0; l--) { // padding for symmetry
                  System.out.print("    "); 
              }
              for (int j = 0; j <= i; j++) { // "left side" of pyramid
                  System.out.printf("%3d ", 1 << j); 
              }
              for (int k = i - 1; k >= 0; k--) { // "right side" of pyramid
                  System.out.printf("%3d ", 1 << k); 
              }
              System.out.println();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多