我写的另外一种思路:;P
package com.itheima;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Demo().printDemo(9);
}
public void printDemo(int n) {
String str = " ";
StringBuilder sb = new StringBuilder();
int i = n;
while (i > 0) {
int j = 0;
while (j < i) {
j++;
sb.insert(0, j);
}
j--;
while (j > 0) {
sb.insert(0, j);
j--;
}
while (j < n - i) {
sb.insert(0, str);
j++;
}
System.out.println(sb.toString());
sb.delete(0, sb.length());
i--;
}
}
}
|