import java.util.Scanner;
class LinXing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您想要打印的菱形的行数大小(奇数)!");
int a = sc.nextInt(); //接收 想要打印的行数(奇数行!)
printLing(a); //打印出菱形的方法!
}
public static void printLing(int a) {
int b = (a + 1)/2; //得到中间行的行数
printShang(b-1); //打印上面部分的星星
printZhong(b); //打印中间的星星
printXia(b-1); //打印下面部分的星星
}
//打印菱形上部分
private static void printShang(int b) {
for (int i = 0; i < b; i++) {
for (int j = 0; j < (b-i); j++) {
System.out.print(" ");
}
for (int k = 0; k < (i*2+1); k++) {
System.out.print("*");
}
System.out.println();
}
}
//打印菱形中间部分
private static void printZhong(int b) {
int x = b*2 -1;
for (int i = 0; i < x; i++) {
System.out.print("*");
}
System.out.println();
}
//打印菱形下面部分
private static void printXia(int b) {
for (int i = 0; i < b; i++) {
for (int j = 0; j < (i+1); j++) {
System.out.print(" ");
}
for (int k = 0; k <((b-i-1)*2 + 1) ; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
|
|