package com.accp.ch1;
import java.util.Scanner;
public class DiamondShape {
/**
* 接收用户要打印的菱形大小,lineSize代表打印(行数+1),lineSize应为偶数。
* @param lineSize
*/
public static void printDiamondShape(int lineSize){
lineSize = lineSize / 2;
for(int i=-lineSize; i<=lineSize; i++){//控制总行数
for(int j=1; j<=Math.abs(i); j++){//打印空格
System.out.print(" ");
}
for(int k=1; k<=2*lineSize+1-(Math.abs(i))*2; k++){//打印*号
System.out.print("*");
}
System.out.println();//每打印完一行换行
}
}
public static void main(String[] args) {
System.out.println("请输入菱形的行数:");
Scanner sc = new Scanner(System.in);
int lines = sc.nextInt();
printDiamondShape(lines);
}
}
自定义菱形打印
最新推荐文章于 2021-05-25 16:21:18 发布