1. 程式人生 > >JAVA列印菱形

JAVA列印菱形

import java.util.Scanner;

public class Demo1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);

		int lineNum = sc.nextInt();

		// 列印菱形 行數為奇數

		// 正三角形行數 總行數/2+1 底下倒立三角形 總行數/2

		int sline = lineNum / 2 + 1;

		int xline = lineNum / 2;
		for (int i = 1; i <= sline; i++) {
			for (int j = 1; j <= sline - i; j++) {
				System.out.print(" ");
			}
			for (int k = 1; k <= 2 * i - 1; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
		for (int i = 1; i <= xline; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(" ");
			}
			for (int k = 1; k <= 2 * (xline - i) + 1; k++) {
				System.out.print("*");
			}

			System.out.println();
		}

	}

}