代码之家  ›  专栏  ›  技术社区  ›  Ashley

正在创建给定格式的三角形序列。。。为什么不显示我的三角形

  •  1
  • Ashley  · 技术社区  · 7 年前

    我正在努力找出我的程序出了什么问题。应打印四个三角形:

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

    以下是我迄今为止编写的代码:

    public static String trianglePatterns(int limit) {
        // Triangle One
        for (int x = 1; x <= limit; x++) {
            for (int y = 1; y <= x; y++) {
                System.out.print(y + " ");
            }
            System.out.print("\n");
        }
        System.out.print("\n");
        // Triangle Two
        while (limit > 0) {
            for (int y = 1; y <= limit; y++) {
                System.out.print(y + " ");
            }
            limit--;
            System.out.println("");
        }
        // Triangle Three
        for (int row = 1; row <= limit; row++) {
            for (int space = 7; space >= 0; space--) {
                System.out.print(" ");
            }
            for (int num = 1; row >= 1; num--) {
                System.out.print(" " + num);
            }
        }
        return "";
    }
    

    我甚至还没有开始第四个,因为我不知道如何开始。我的程序将正确运行并显示前两个三角形,但在运行时不包括第三个三角形。问题的原因是什么?如何开始第四个三角形?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Russiancold    7 年前

    limit 在三角形两个循环中为0,但在第三个循环中中断条件 row <= limit . 这就是为什么它不起作用。
    phflack 注意,您不需要将整数强制转换为 String 打印时。只需通过 int print() . 阅读更多信息 docs .

        2
  •  0
  •   Juan Carlos Mendoza    7 年前

    需要指出的是:

    1. 你的方法可能无效。
    2. System.out.print("\n"); = System.out.println();
    3. 您正在修改 limit

    以下是我的尝试:

    public static void trianglePatterns(final int limit) {
        // Triangle One
        for (int x = 1; x <= limit; x++) {
            for (int y = 1; y <= x; y++) {
                System.out.print(y + " ");
            }
            System.out.println();
        }
        System.out.println();
    
        // Triangle Two
        for (int x = limit; x > 0; x--) {
            for (int y = 1; y <= x; y++) {
                System.out.print(y + " ");
            }
            System.out.println();
        }
        System.out.println();
    
        // Triangle Three
        // first printint spaces of each row and then the numbers
        for (int x = 1; x <= limit; x++) {
            for (int space = (limit - x); space > 0; space--) {
                System.out.print("  ");
            }
    
            for (int num = x; num > 0; num--) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    
        System.out.println();
        // Triangle Four
        for (int x = limit; x >= 0; x--) {
            for (int space = (limit - x); space > 0; space--) {
                System.out.print("  ");
            }
    
            for (int num = 1; num <= x; num++) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
    

    的输出 trianglePatterns(5)

    1 
    1 2 
    1 2 3 
    1 2 3 4 
    1 2 3 4 5 
    
    1 2 3 4 5 
    1 2 3 4 
    1 2 3 
    1 2 
    1 
    
            1 
          2 1 
        3 2 1 
      4 3 2 1 
    5 4 3 2 1 
    
    1 2 3 4 5 
      1 2 3 4 
        1 2 3 
          1 2 
            1 
    
    推荐文章