我必须只使用嵌套的for循环生成以下输出:
—– —– 1
—- —- 333
— 55555 —
–7777777–
-999999999-
我不能使用任何while或if语句
这是我的代码:
public static void printDesign() {
//for loop for the number of lines
for (int i = 1; i <= 9; i++) {
//for loop for the left -
for (int j = 1; j <= 6 - i; j++) {
System.out.print("-");
}
//for loop for #'s
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print(i);
}
//for loop for the right -
for (int x = 1; x <= 6 - i; x++) {
System.out.print("-");
}
System.out.println();
}
}
这就是它产生的:
—– —– 1
—- —- 222
— 33333 —
–4444444–
-555555555-
66666666666
7777777777777
888888888888888
99999999999999999
我怎样才能得到它只产生奇数?