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
おすすめ