具有特定输出的三重嵌套For循环(java)

Cuc*_*inn 4 java algorithm for-loop

我需要使用3"for"循环输出来编写一些java

122333444455555

22333444455555

333444455555

444455555

55555

我到目前为止的代码:

public static void problemFour() {
      for(int i = 5; i >= 1; i--) {
         for(int a = 1; a <= i; a++) {
            for(int b = 1; b <= a; b++) {
               System.out.print(a);
            }
         }
         System.out.println();
      }
   }
Run Code Online (Sandbox Code Playgroud)

这输出

111112222333445
11111222233344
111112222333
111112222
11111
Run Code Online (Sandbox Code Playgroud)

我已经转换了很多++的's,'s,>',5和1的组合.

我很困惑,如果有人能指出我正确的方向,那就太棒了.

Sau*_*ahu 5

你在线的开始方式以及数字(此处字符)重复的次数上犯了错误.修复它:

for(int i = 1; i <= 5; i++) {          // Each iteration for one line
    for(int a = i; a <= 5; a++) {      // starts with a for ith line
        for(int b = 1; b <= a; b++) {  // a times `a` digit
            System.out.print(a);
        }
    }
    System.out.println();
}
Run Code Online (Sandbox Code Playgroud)

简单地解决您的问题,首先要考虑打印此模式:

12345
2345
345
45
5
Run Code Online (Sandbox Code Playgroud)

然后扩展它: 在最里面的循环中,将重复的代码等于数字时间,使用:

for(int b = 1; b <= a; b++) {  // a times `a` digit
     System.out.print(a);
}
Run Code Online (Sandbox Code Playgroud)