直接上代码:
public class PrintedDiamondShape {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print('*');
}
System.out.println(" ");
}
for (int a = 1; a <= 6; a++) {
for (int d = 1; d < a; d++) {
System.out.print(" ");
}
for (int b = 6; b >= a; b--) {
System.out.print("*");
}
System.out.println(" ");
}
}
}
使用了多层的嵌套循环实现这一需求:
效果如下:
完美!