package com.itheima;
public class Diamond {
public static void main(String[] args) {
System.out.println("1,打印一个菱形");
printDiamond(1,1);
System.out.println("2,打印两个菱形");
printDiamond(1,2);
System.out.println("3,打印四个个菱形");
printDiamond(2,2);
System.out.println("4,打印任意行列数的菱形,这里以5*7为例");
printDiamond(5,7);
}
public static void printDiamond(int hang,int lie){
printSpace(5 - 1);
printWord(2 * 1 - 1);
printSpace(5 - 1);
for (int j = 1; j < lie; j++) {
printSpace(4 - 1);
printWord(2 * 1 - 1);
printSpace(5 - 1);
}
System.out.println();
for (int x = 0; x < hang; x++) {//对行进行循环
//打印上半段
for (int i = 2; i <= 4; i++) {
printSpace(5 - i);
printWord(2 * i - 1);
printSpace(5 - i);
for (int j = 1; j < lie; j++) {
printSpace(4 - i);
printWord(2 * i - 1);
printSpace(5 - i);
}
System.out.println();//每打一行就换行
}
//打印中间一行
printWord(8*lie+1);
System.out.println();
//打印下半段
for (int i = 4; i > 0; i--) {
printSpace(5 - i);
printWord(2 * i - 1);
printSpace(5 - i);
for (int j = 1; j < lie; j++) {
printSpace(4 - i);
printWord(2 * i - 1);
printSpace(5 - i);
}
System.out.println();//每打一行就换行
}
}
}
public static void printSpace(int m){//打印空格的方法
for (int i = 0; i < m; i++) {
System.out.print(" ");
}
}
public static void printWord(int m){//打印*的方法
for (int i = 0; i < m; i++) {
System.out.print("*");
}
}
}
|