打印任一行数的菱形

package ClassicQuestion;

import java.util.Scanner;

/**
 * 打印任一行的菱形
 * 重点:菱形最长的一行的*数是:(line/2+1)*2-1
 * @author 11654
 *
 */
public class PrintRhomb {
	public static void main(String[] args) {
		System.out.println("输入要打印的菱形的行数");
		Scanner in = new Scanner(System.in);
		int line = in.nextInt();
		//打印上半部分
		for (int i = 1; i <= line/2+1; i++) {
			for (int j = 1; j <= (line/2+1)-i; j++) {
				System.out.print(" ");
			}
			for (int k = 1; k <= 2*i-1; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
		//打印下半部分。我感觉下半部分比上半部分要难print一些
		for (int i = 1; i <= line/2; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(" ");
			}
			for (int k = 1; k <= (line/2+1)*2 - (2*i+1); k++) {
				System.out.print("*");
			}
			System.out.println();	//每打印一行图形,换行
		}
		in.close();
	}
}

在这里插入图片描述

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页