一、打印效果

二、实现代码汇总

为了方便初学者对代码的理解,建议熟练t1到t5为各种三角的打印,然后再进行菱形的打印实现。
  1 package circulationDemo;
  2 
  3 import java.util.Scanner;
  4 
  5 public class Test {
  6     public static void main(String[] args) {
  7         Scanner input = new Scanner(System.in);
  8         System.out.println("请输入要打印的行数:");
  9         int num = input.nextInt();
 10         t1(num);// 正直角
 11         split();
 12         t2(num);// 倒直角
 13         split();
 14         t3(num);// 等腰三角
 15         split();
 16         t4(num);// 等腰倒三角++
 17         split();
 18         t5(num);// 等腰倒三角--
 19         split();
 20         t6(num);// 实心菱形
 21         split();
 22         t7(num);// 空心菱形(1)
 23         split();
 24         t8(num);// 空心菱形(2)
 25         split();
 26         t9(num);// 空心菱形(3)
 27     }
 28 
 29     private static void t9(int num) {
 30         for (int k = 1; k <= num; k++) {
 31             for (int i = 1; i <= (num - k); i++) {
 32                 System.out.print(" ");
 33             }
 34             System.out.print("*");
 35             for (int i = 1; i <= ((k - 2) * 2 + 1); i++) {
 36                 System.out.print(" ");
 37             }
 38             if (k != 1) {
 39                 System.out.print("*");
 40             }
 41             System.out.println();
 42         }
 43         for (int k = num-1; k >= 1; k--) {
 44             for (int i = 1; i <= (num - k); i++) {
 45                 System.out.print(" ");
 46             }
 47             System.out.print("*");
 48             for (int i = 1; i <= ((k - 2) * 2 + 1); i++) {
 49                 System.out.print(" ");
 50             }
 51             if (k != 1) {
 52                 System.out.print("*");
 53             }
 54             System.out.println();
 55         }
 56     }
 57 
 58     private static void t8(int num) {
 59         // 上半部分
 60         for (int i = 1; i <= num; i++) {
 61             // 打印上半部分空格
 62             for (int j = 1; j <= num - i; j++) {
 63                 System.out.print(" ");
 64             }
 65             // 打印原本实心的*部分
 66             for (int j = 1; j <= 2 * i - 1; j++) {
 67                 // 仅在一行的开头和末尾打印*
 68                 if (j == 1 || j == 2 * i - 1) {
 69                     System.out.print("*");
 70                 } else {
 71                     System.out.print(" ");
 72                 }
 73             }
 74             System.out.println();
 75         }
 76         // 下半部分
 77         for (int i = 1; i <= num - 1; i++) {
 78             for (int j = 1; j <= i; j++) {
 79                 System.out.print(" ");
 80             }
 81             for (int j = 1; j <= 2 * (num - i - 1) + 1; j++) {
 82                 if (j == 1 || j == 2 * (num - i - 1) + 1) {
 83                     System.out.print("*");
 84                 } else {
 85                     System.out.print(" ");
 86                 }
 87             }
 88             System.out.println();
 89         }
 90 
 91     }
 92 
 93     private static void t7(int num) {
 94         // 打印上半部分
 95         for (int i = 1; i <= num; i++) {
 96             // 打印空格
 97             for (int j = 1; j <= (num - i); j++) {
 98                 System.out.print(" ");
 99             }
100             // 打印*的个数
101             if (i == 1) {
102                 System.out.print("*");
103             } else {
104                 System.out.print("*");
105                 // 打印菱形里的空格
106                 for (int j = 1; j <= (2 * i - 3); j++) {
107                     System.out.print(" ");
108                 }
109                 System.out.print("*");
110             }
111 
112             System.out.println();
113         }
114         // 打印下半部分
115         for (int i = num - 1; i > 0; i--) {
116             // 打印空格
117             for (int j = 1; j <= (num - i); j++) {
118                 System.out.print(" ");
119             }
120             // 打印*的个数
121             if (i == 1) {
122                 System.out.print("*");
123             } else {
124                 System.out.print("*");
125                 // 打印菱形里的空格
126                 for (int j = 1; j <= (2 * i - 3); j++) {
127                     System.out.print(" ");
128                 }
129                 System.out.print("*");
130             }
131             System.out.println();
132         }
133     }
134 
135     private static void t6(int num) {
136         // 上半部分
137         for (int i = 1; i <= num; i++) {
138             for (int j = 1; j <= num - i; j++) {
139                 System.out.print(" ");
140             }
141             for (int j = 1; j <= 2 * i - 1; j++) {
142                 System.out.print("*");
143             }
144             System.out.println();
145         }
146         // 下半部分
147         for (int i = 1; i <= num - 1; i++) {
148             for (int j = 1; j <= i; j++) {
149                 System.out.print(" ");
150             }
151             for (int j = 1; j <= 2 * (num - 1 - i) + 1; j++) {
152                 System.out.print("*");
153             }
154             System.out.println();
155         }
156     }
157 
158     private static void t5(int num) {
159         for (int i = num; i > 0; i--) {
160             for (int j = 1; j <= num - i; j++) {
161                 System.out.print(" ");
162             }
163             for (int j = 1; j <= 2 * i - 1; j++) {
164                 System.out.print("*");
165             }
166             System.out.println();
167         }
168     }
169 
170     private static void t4(int num) {
171         for (int i = 1; i <= num; i++) {
172             for (int j = 1; j <= i - 1; j++) {
173                 System.out.print(" ");
174             }
175             for (int j = 1; j <= 2 * (num - i) + 1; j++) {
176                 System.out.print("*");
177             }
178             System.out.println();
179         }
180     }
181 
182     private static void t3(int num) {
183         for (int i = 1; i <= num; i++) {
184             for (int j = 1; j <= num - i; j++) {
185                 System.out.print(" ");
186             }
187             for (int j = 1; j <= 2 * i - 1; j++) {
188                 System.out.print("*");
189             }
190             System.out.println();
191         }
192     }
193 
194     private static void split() {
195         System.out.println("---------------");
196     }
197 
198     private static void t2(int num) {
199         for (int i = 1; i <= num; i++) {
200             for (int j = 1; j <= num - i + 1; j++) {
201                 System.out.print("*");
202             }
203             System.out.println();
204         }
205     }
206 
207     private static void t1(int num) {
208         for (int i = 1; i <= num; i++) {
209             for (int j = 1; j <= i; j++) {
210                 System.out.print("*");
211             }
212             System.out.println();
213         }
214     }
215 
216 }