在本文档中,首先,我们将了解如何使用 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中文网其他相关文章!