Java 打印一个菱形

题目:打印出如下图案(菱形)

   *

  ***

 *****

*******

 *****

  ***

   *

import java.util.Scanner;

public class Demo05 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入菱形层数(必须为奇数):");
		int n = input.nextInt();//总层数
		int p = 0;//空格数
		int s = -1;//*的个数
		
		for(int i=1; i<=n/2+1; i++) { //上半部分
			s += 2; 
			p = (n-s) / 2;
			for(int j=1; j<=p; j++) 
				System.out.print(" ");
				for(int k=1; k<=s; k++)
					System.out.print("*");
			System.out.println();
		}
		
		for(int i=1; i<=n/2; i++) { //下半部分
			s -= 2; 
			p = (n-s) / 2;
			for(int j=1; j<=p; j++) 
				System.out.print(" ");
				for(int k=1; k<=s; k++)
					System.out.print("*");
			System.out.println();
		}
	}
}

结果如下

发布了35 篇原创文章 · 获赞 5 · 访问量 859

猜你喜欢

转载自blog.csdn.net/m0_43443133/article/details/104802912