java打印实心10*10正方形, 空心10*10正方形

 1 public class PrintSquare {
 2 
 3     public static void main(String[] args) {
 4         printSolidSquare(10);
 5         System.out.println("==我是一条分割线==");
 6         printHollowSquare(10);
 7     }
 8 
 9     // 打印空心正方形
10     private static void printHollowSquare(int n) {
11         for (int i = 1; i <= n; i++) {
12             for (int j = 1; j <= n; j++) {
13                 if (i == 1 || i == n || j == 1 || j == n) {
14                     System.out.print("* ");// 这里有一个空格
15                 } else {
16                     System.out.print("  ");// 这里有两个空格
17                 }
18             }
19             System.out.println();
20         }
21     }
22 
23     // 打印实心正方形
24     private static void printSolidSquare(int n) {
25         for (int i = 1; i <= n; i++) {
26             for (int j = 1; j <= n; j++) {
27                 System.out.print("* ");// 这里有一个空格
28             }
29             System.out.println();
30         }
31     }
32 
33 }

 咱们来看看效果(不多说,上图):

感觉效果还是不错的,但是就是不知道为什么我把星号(*)换成其他字符(比如❤)就不行了,>﹏<

猜你喜欢

转载自www.cnblogs.com/zxfei/p/10702624.html