Java 嵌套循环反向计数器位置
嘿,伙计们,我刚刚学会了如何做嵌套循环。这就是我所做的Java 嵌套循环反向计数器位置,java,Java,嘿,伙计们,我刚刚学会了如何做嵌套循环。这就是我所做的 public class TEst_1 { public static void main(String[] args) { final int max = 10; for (int row = 1; row <= max; row++) { for (int col = 1; col <= row; col++) System.out.print(col);
public class TEst_1 {
public static void main(String[] args) {
final int max = 10;
for (int row = 1; row <= max; row++) {
for (int col = 1; col <= row; col++)
System.out.print(col);
System.out.println();
}
}
但我有一个问题,我该如何扭转这些数字?我的意思是从右边数列
1
21
321
4321
54321
654321
7654321
87654321
987654321
10987654321
您可以在内部循环中反向计数:
for (int col = row; col > 0; col--)
从最高的数字开始,递减,直到达到最低的数字
for (int col = row; col >= 1; col--)
哦我现在明白逻辑了。我非常感谢你的帮助。
for (int col = row; col >= 1; col--)