我写了这些嵌套for循环,在一行显示1,在下一行显示31,依此类推,直到它到达97531
。有人可以告诉我如何反转它,以便97531
打印在第一行,然后7531
,依此类推。基本上只是翻转整个输出。
public class Tester {
public static void main(String[] args){
for (int i = 1; i <= 5; i++) {
int currentNum = 2 * i - 1;
for (int j = 1; j <= i; j++) {
System.out.print(currentNum);
currentNum = currentNum - 2;
}
System.out.println();
}
}
}
答案 0 :(得分:3)
试试这个
for (int i = 5; i >= 1; i--) {
int currentNum = 2 * i - 1;
for (int j = 1; j <= i; j++) {
System.out.print(currentNum);
currentNum = currentNum - 2;
}
System.out.println();
}
答案 1 :(得分:0)
你走了:
public class Tester {
public static void main(String[] args){
for (int i = 5; i >= 1; i--) {
int currentNum = 2 * i - 1;
for (int j = i; j >= 1; j--) {
System.out.print(currentNum);
currentNum = currentNum - 2;
}
System.out.println();
}
}
}