首页 >Java >java教程 >Java 中的星形图案

Java 中的星形图案

WBOY
WBOY原创
2024-08-30 16:24:43861浏览

在本文档中,首先,我们将了解如何使用 Java 编程来处理星形图案程序。星型模式是一种常见的Java模式程序,广泛用于提高逻辑思维和流程控制知识。在 Java 编程中,您需要使用两个循环或三个循环(取决于程序)来显示星形图案。第一个循环是外循环,第二个循环是内循环,分别显示行和列。

本文档对于那些想了解设计模式以提高面向对象设计和开发能力的Java程序员有帮助。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

星形图案示例

让我们讨论一些例子来轻松理解 Java 中模式的概念。

示例#1

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例#2

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例#3

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例#4

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例#5

代码:

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(" ");
}
}
}
}

输出:

Java 中的星形图案

示例#6

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例#7

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例#8

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例 #9

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例#10

代码:

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();
}
}
}

输出:

Java 中的星形图案

示例#11

代码:

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(" ");
}
}
}
}

输出:

Java 中的星形图案

示例#12

代码:

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 编程语言中不同类型的模式。这些模式是经验丰富的面向对象软件设计人员使用的最佳实践。用户可以利用这些设计模式来讨论面向对象的软件设计。借助这些模式,没有经验的开发人员可以越来越快地学习软件设计。

以上是Java 中的星形图案的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn