如何将另一个Java数字三角形并排放置

huangapple go评论82阅读模式
英文:

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        1 2 3
1 2      1 2 3 4

我不知道如何得到这个输出,帮助和解释将会很有帮助,因为我也想用类似的方法处理其他类型的内容。

英文:

So i was trying this code i found on the internet that let me make number triangles, the codes are like so

public class StarsAndDraws {

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

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

the output looks like this

      1
    1 2
  1 2 3
1 2 3 4

but this is the output im looking for

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

i have no idea how, help and explanation is appreciated because id like to do this to other kinds of stuff aswell

答案1

得分: -1

为了打印第一个 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();
    }
  }
}
英文:

To print first 1/ 1 2, it is needed to define another loop for that one too.

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

huangapple
  • 本文由 发表于 2020年10月27日 16:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64550920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定