我需要使用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();
}
}下面的输出
111112222333445
11111222233344
111112222333
111112222
11111我已经交换了许多++的组合--'s,<'s,>'s,5‘和1's。
我被困住了,如果有人能给我指出正确的方向,那就太好了。
发布于 2017-10-02 13:58:37
查看内联注释。
public static void main(String[] args) {
for (int i = 5, j = 1; i >= 1; i--, j++) { // Introduce a new variable j
for (int a = j; a <= 5; a++) { // change a=1 to a=j & a<=i to a<=5
for (int b = 1; b <= a; b++) {
System.out.print(a);
}
}
System.out.println();
}
}输出:
122333444455555
22333444455555
333444455555
444455555
55555https://stackoverflow.com/questions/46520008
复制相似问题