Java print a diamond

Title: Print out the following pattern (diamonds)

   *

  ***

 *****

*******

 *****

  ***

   *

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();
		}
	}
}

The results are as follows

Published 35 original articles · won praise 5 · Views 859

Guess you like

Origin blog.csdn.net/m0_43443133/article/details/104802912