在本文檔中,首先,我們將了解如何使用 Java 程式設計來處理星形圖案程式。星型模式是一種常見的Java模式程序,廣泛用於提升邏輯思考和流程控制知識。在 Java 程式設計中,您需要使用兩個循環或三個循環(取決於程式)來顯示星形圖案。第一個循環是外循環,第二個循環是內循環,分別顯示行和列。
本文檔對於那些想了解設計模式以提高物件導向設計和開發能力的Java程式設計師有幫助。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗讓我們討論一些例子來輕鬆理解 Java 中模式的概念。
代碼:
import java.util.Scanner; public class FirstPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = 1; m <= myrows; m++) { for (int n = 1; n <= m; n++) { System.out.print("*"); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class SecondPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = 1; m <= myrows; m++) { for (int n = myrows; n > m; n--) { System.out.print("*"); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class ThirdPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = 1; m <= myrows; m++) { for (int n = 1; n < m; n++) { System.out.print(" "); } for (int p=myrows; p>=m; p--) { System.out.print("*"); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class FourthPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = 1; m <= myrows; m++) { for (int n=myrows; n>m; n--) { System.out.print(" "); } for (int p=1; p<=(m * 2) -1; p++) { System.out.print("*"); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class FifthPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m=myrows; m>=1; m--) { for (int n=1; n<=(m * 2) -1; n++) { System.out.print("*"); } System.out.println(); for (int p=myrows; p>=m; p--) { System.out.print(" "); } } } }
輸出:
代碼:
import java.util.Scanner; public class SixthPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m=1; m<=myrows; m++) { for (int n=myrows; n>m; n--) { System.out.print(" "); } for (int p=1; p<=(m * 2) -1; p++) { System.out.print("*"); } System.out.println(); } for (int m=myrows-1; m>=1; m--) { for (int n=myrows-1; n>=m; n--) { System.out.print(" "); } for (int p=1; p<=(m * 2) -1; p++) { System.out.print("*"); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class SeventhPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = 1; m <= myrows; m++) { for (int n=1; n<=m; n++) { if( n == 1 || n == m || m == myrows) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class EighthPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = myrows; m >= 1; m--) { for (int n = m; n >= 1; n--) { System.out.print("*"); } System.out.println(); } for (int m = 2; m <= myrows; m++) { for (int n = m; n >= 1; n--) { System.out.print("*"); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class NinthPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = 1; m <= myrows; m++) { for (int n = myrows-1; n>=m; n--) { System.out.print(" "); } for (int p = 1; p <= myrows; p++) { System.out.print("*"); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class TenthPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = 1; m <= myrows; m++) { for (int n = 1; n < m; n++) { System.out.print(" "); } for (int p = m; p <= myrows; p++) { System.out.print("* "); } System.out.println(); } for (int m = myrows-1; m >= 1; m--) { for (int n = 1; n < m; n++) { System.out.print(" "); } for (int p = m; p <= myrows; p++) { System.out.print("* "); } System.out.println(); } } }
輸出:
代碼:
import java.util.Scanner; public class ElevenPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m=myrows; m>=1; m--) { for (int n=1; n <=(m * 2) -1; n++) { if( n == 1 || n == (m * 2) -1 || m == myrows) System.out.print("*"); else System.out.print(" "); } System.out.println(); for (int p = myrows; p >= m; p--) { System.out.print(" "); } } } }
輸出:
代碼:
import java.util.Scanner; public class TwelthPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please provide number of rows to print... "); int myrows = scanner.nextInt(); System.out.println("\nThe star pattern is... "); for (int m = 1; m <= myrows; m++) { for (int n = 1; n <= myrows; n++) { System.out.print("*"); } System.out.println(); } } }
輸出:
到目前為止,我們已經討論了 Java 程式語言中不同類型的模式。這些模式是經驗豐富的物件導向軟體設計人員使用的最佳實踐。使用者可以利用這些設計模式來討論物件導向的軟體設計。借助這些模式,沒有經驗的開發人員可以越來越快地學習軟體設計。
以上是Java 中的星形圖案的詳細內容。更多資訊請關注PHP中文網其他相關文章!