繁体   English   中英

我如何并排放置另一个java数字三角形

[英]How do i put the other java number triangle side by side

所以我正在尝试我在互联网上找到的这段代码,它可以让我制作数字三角形,代码就像这样

public class StarsAndDraws {

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

            }
            System.out.println();
        }
    }
 
}

输出看起来像这样

      1
    1 2
  1 2 3
1 2 3 4

但这是我正在寻找的输出

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

我不知道如何,感谢帮助和解释,因为我也喜欢对其他类型的东西这样做

要打印第一个1/ 1 2 ,还需要为该循环定义另一个循环。

class Main {
  public static void main(String[] args) {
    for (int i = 1; i <= 4; i++) {
      if (i < 3) {
        System.out.print(" ".repeat(9));
      } else {
        System.out.print(" ".repeat((4 - i) * 2));
        for (int j = 1; j <= i - 2; j ++) {
          System.out.print(j);
          System.out.print(" ");
        }
        System.out.print(" ".repeat(6));
      }
      System.out.print(" ".repeat(2 * (4 - i)));
      for (int j = 1; j <= i; j ++) {
        System.out.print(j);
        System.out.print(" ");
      }
      System.out.println();
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM