数据结构与算法-打印菱形算法

2019-10-26 21:24:43 浏览数 (1)

代码语言:javascript复制
package *;

/**
 * @program: data-structure
 * @description: 打印菱形算法
 * @author: ChenWenLong
 * @create: 2019-09-10 14:38
 **/
public class Rhombus {

    public static void main(String[] args) {
        //printHollowRhombus(10);
        printSolidRhombus(10);
    }

    /**
     * 功能描述:
     * 〈打印指定行数line的空心菱形〉
     *
     * @params : [line]
     * @return : void
     * @author : cwl
     * @date : 2019/9/10 15:12
     */
    public static void printHollowRhombus(int line) {
        if (line % 2 == 0) {
            line  ;// 计算菱形大小
        }
        for (int i = 0; i < line / 2   1; i  ) {
            for (int j = line / 2   1; j > i   1; j--) {
                System.out.print("  ");// 输出左上角位置的空白
            }
            for (int j = 0; j < 2 * i   1; j  ) {
                if (j == 0 || j == 2 * i) {
                    System.out.print("* ");// 输出菱形上半部边缘
                } else {
                    System.out.print("  ");// 输出菱形上半部空心
                }
            }
            System.out.println("");		//换行
        }
        for (int i = line / 2   1; i < line; i  ) {
            for (int j = 0; j < i - line / 2; j  ) {
                System.out.print("  ");// 输出菱形左下角空白
            }
            for (int j = 0; j < 2 * line - 1 - 2 * i; j  ) {
                if (j == 0 || j == 2 * (line - i - 1)) {
                    System.out.print("* ");// 输出菱形下半部边缘
                } else {
                    System.out.print("  ");// 输出菱形下半部空心
                }
            }
            System.out.println("");		//换行
        }
    }
    
    /**
     * 功能描述: 
     * 〈打印指定行数line的实心菱形〉
     *
     * @params : [line]
     * @return : void
     * @author : cwl
     * @date : 2019/9/10 15:14
     */
    public static void printSolidRhombus(int line){
        for(int i = 0;i < line;i  ){
            for(int k = 0;k < line-1 - i;k  ){
                System.out.print(" ");
            }
            for(int j = 0;j < i   1;j  ){
                System.out.print("* ");
            }
            System.out.println();
        }
        //下半部分
        for(int i = 0;i < line-1;i  ){
            for(int k = 0;k < i   1;k  ){
                System.out.print(" ");
            }
            for(int j = 0;j < line-1 - i;j  ){
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

0 人点赞