Java每行循环次数平方(倒序)

Java Loop Number square ( reversed order ) each line

所以我想知道我该怎么做...

我在互联网上找到了这段代码,它给我一个数字方块模式

import java.util.Scanner;

public class Square {


    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int a;
        
        System.out.print("Input :");
        a = sc.nextInt();
        
        for (int iOuter = 1; iOuter <= a; iOuter++) {
            
            for (int i = 1; i <= a; i++) {
                
                System.out.print(i);
                
            }
            System.out.println();
        }

    }
    
}

如果我输入 5

,这就是输出
12345
12345
12345
12345
12345

我正在寻找这个输出

12345
54321
12345
54321
12345

提前致谢

试试这个代码:

for (int iOuter = 1; iOuter <= a; iOuter++) {
            
            for (int i = 1; i <= a; i++) {
                if (iOuter % 2 == 0) { // if is even
                    System.out.print(1 + a - i);
                }else {// if is odd
                    System.out.print(i);
                }
            }
            System.out.println();
        }

这会对奇数行和偶数行产生不同的结果。

这只是将值转换为字符串并使用 StringBuilder 将其反转。

int val = 54321;
StringBuilder sb = new StringBuilder().append(val);
for(int i = 0; i < 5; i++) {
   System.out.println(sb.reverse());
}

版画

12345
54321
12345
54321
12345