数据结构与算法-打印正方形算法

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

代码语言:javascript复制
package *;

/**
 * @program: data-structure
 * @description: 正方形
 * @author: ChenWenLong
 * @create: 2019-09-10 14:46
 **/
public class Square {

    public static void main(String[] args) {
        printSolidSquare(10);
        printHollowSquare(10);
    }

    /**
     * 功能描述:
     * 〈答应实心的正方形,rowNum为你想要打印正方形的行数〉
     *
     * @params : [rownum]
     * @return : void
     * @author : cwl
     * @date : 2019/9/10 14:46
     */
    public static void printSolidSquare(int rowNum) {
        if (rowNum > 1) {
            for (int i = 1; i <= rowNum; i  ) {
                for (int j = 1; j <= rowNum; j  ) {
                    System.out.print("*");
                }
                System.out.println();
            }
        } else {
            System.out.println("数字应大于1");
        }
    }

    /**
     * 功能描述:
     * 〈打印空心正方形〉
     *
     * @params : [rownum]
     * @return : void
     * @author : cwl
     * @date : 2019/9/10 14:50
     */
    public static void printHollowSquare(int rowNum) {
        if (rowNum > 1) {
            for (int i = 1; i <= rowNum; i  ) {
                for (int j = 1; j <= rowNum; j  ) {
                    if (i == 1 || i == rowNum) {
                        System.out.print("*");
                    } else {
                        if (j == 1 || j == rowNum) {
                            System.out.print("*");
                        } else {
                            System.out.print(" ");
                        }
                    }
                }
                System.out.println();
            }
        } else {
            System.out.println("数字应大于1");
        }
    }
}

0 人点赞