數據結構與算法-打印正方形算法
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"); } } }