Java打印实心、空心的三角形和菱形

1.实心三角形

代码:

 1 import java.util.Scanner;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         int rows;
 7         Scanner sc = new Scanner(System.in);
 8         System.out.println("Please input rows:");
 9         rows = sc.nextInt();
10         sc.close();
11         for (int i = 1; i <= rows; i++) {//控制打印行数
12             for (int j = 1; j <= rows - i; j++) {//控制每行打印空格数
13                 System.out.print(" ");
14             }
15             for (int j = 1; j <= i * 2 - 1; j++) {//控制打印星号数
16                 System.out.print("*");
17             }
18             System.out.println();//每打一行,换行
19         }
20     }
21 
22 }
View Code

2.空心三角形

代码:

 1 import java.util.Scanner;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         int rows;
 6         Scanner sc = new Scanner(System.in);
 7         System.out.println("Please input rows:");
 8         rows = sc.nextInt();
 9         sc.close();
10         for (int i = 1; i <= rows; i++) {
11             for (int j = 1; j <= rows - i; j++) {
12                 System.out.print(" ");
13             }
14             for (int j = 1; j <= 2 * i - 1; j++) {
15                 if (i == 1 || i == rows) { // 如果是第一行或最后一行,打印所有星号
16                     System.out.print("*");
17                 } else if (j == 1 || j == 2 * i - 1) { // 如果是每行的第一个或者最后一个,打印星号
18                     System.out.print("*");
19                 } else { // 其余打印空格
20                     System.out.print(" ");
21                 }
22             }
23             System.out.println();
24         }
25     }
26 }
View Code

3.实心菱形

代码:

 1 import java.util.Scanner;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         int rows;
 6         Scanner sc = new Scanner(System.in);
 7         System.out.println("Please input odd rows:");// 只能是奇数行
 8         rows = (sc.nextInt() + 1) / 2;// 上半部分行数
 9         sc.close();
10         for (int i = 1; i <= rows; i++) {
11             for (int j = 1; j <= rows - i; j++) {
12                 System.out.print(" ");
13             }
14             for (int j = 1; j <= 2 * i - 1; j++) {
15                 System.out.print("*");
16             }
17             System.out.println();
18         }
19         for (int i = rows - 1; i >= 1; i--) { // 下半部分不可重复打印上半部分最后一行,i=rows-1)
20             for (int j = 1; j <= rows - i; j++) {
21                 System.out.print(" ");
22             }
23             for (int j = 1; j <= 2 * i - 1; j++) {
24                 System.out.print("*");
25             }
26             System.out.println();
27         }
28     }
29 }
View Code

4.空心菱形

代码:

 1 import java.util.Scanner;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         int rows;
 6         Scanner sc = new Scanner(System.in);
 7         System.out.println("Please input odd rows:");// 只能是奇数行
 8         rows = (sc.nextInt() + 1) / 2;// 上半部分行数
 9         sc.close();
10         sc.close();
11         for (int i = 1; i <= rows; i++) {
12             for (int j = 1; j <= rows - i; j++) {
13                 System.out.print(" ");
14             }
15             for (int j = 1; j <= 2 * i - 1; j++) {
16                 if (i == 1) {
17                     System.out.print("*");
18                 } else if (j == 1 || j == 2 * i - 1) {
19                     System.out.print("*");
20                 } else {
21                     System.out.print(" ");
22                 }
23             }
24             System.out.println();
25         }
26         for (int i = rows - 1; i >= 1; i--) {// 此处只需i=rows-1即可
27             for (int j = 1; j <= rows - i; j++) {
28                 System.out.print(" ");
29             }
30             for (int j = 1; j <= 2 * i - 1; j++) {
31                 if (i == 1) {
32                     System.out.print("*");
33                 } else if (j == 1 || j == 2 * i - 1) {
34                     System.out.print("*");
35                 } else {
36                     System.out.print(" ");
37                 }
38             }
39             System.out.println();
40         }
41     }
42 }
View Code
posted @ 2019-02-28 21:56  Link_Chen  阅读(1683)  评论(0编辑  收藏  举报