public class 空星菱形2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数组");
int k =sc.nextInt();
demo5(k);//空星菱形
System.out.println("实芯菱形");
demo1(k);
}
private static void demo5(int length) {
//int length=5;
for (int i = 1; i <length; i++) {
for (int j = 0; j <length-1-i; j++) {//空格数
System.out.print(" ");
}
for (int j = 1; j <=i*2-1; j++) {//星星数 j不能等于0,不然i*2-1等于负数
if(j==1 || j==i*2-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
//打印倒着的星星
for (int i =length-2; i>0; i--) {
for (int j =1; length-1-i>=j; j++) {// 行数等于几空格就打印几次 2
System.out.print(" ");
}
for (int j = 1; j <=i*2-1; j++) {
/*System.out.print("*");*/
if(j==1 || j==i*2-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
private static void demo1(int length) {
//int length=5;
for (int i = 1; i <length; i++) {
for (int j = 0; j <length-1-i; j++) {//空格数
System.out.print(" ");
}
for (int j = 1; j <=i*2-1; j++) {//星星数 j不能等于0,不然i*2-1等于负数
System.out.print("*");
}
System.out.println();
}
//打印倒着的星星
for (int i =length-2; i>0; i--) {
for (int j =1; length-1-i>=j; j++) {// 行数等于几空格就打印几次 2
System.out.print(" ");
}
for (int j = 1; j <=i*2-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
|