输出空心菱形

import java.util.*;
class diamond {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("please enter an integer: ");
		int row = sc.nextInt();
//打印上半三角
		for (int i = 1; i <= row; i++) {
			for (int j = 1; j <= row - i; j++) {
				System.out.print(" ");
			}
			for (int k = 1; k <= 2 * i - 1; k++) {
				if (k == 1 || k == 2 * i -1) {
					System.out.print("*");
				}else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}

//打印下半三角
		for (int i = 1; i < row; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(" ");
			}
			for (int k = 1; k <= 2 * row - 1 - 2 * i; k++) {
				if (k == 1 || k == 2 * row - 1 - 2 * i) {
					System.out.print("*");
				}else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		
	}
}

 

你可能感兴趣的:(java)