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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM