Wzory gwiazd w Javie - Top 12 przykładów wzorów gwiazd w Javie

Spisie treści:

Anonim

Wprowadzenie do wzorców gwiezdnych w Javie

W tym dokumencie najpierw zobaczymy, jak używać programowania Java do pracy z programami wzorców gwiazd. Wzorce gwiaździste są jednym z popularnych programów wzorcowych Java, które są szeroko stosowane w celu poprawy logicznego myślenia i wiedzy na temat kontroli przepływu. Musisz użyć dwóch lub trzech pętli (w zależności od programów), aby pokazać wzorce gwiazd w Programowaniu Java. Pierwsza pętla jest pętlą zewnętrzną, a druga pętla jest pętlą wewnętrzną, która pokazuje odpowiednio wiersze i kolumny.

Dokument ten jest przydatny dla programistów Java, którzy chcą poznać wzorce projektowe jako sposób na poprawę swoich obiektowych możliwości projektowania i programowania.

Przykłady wzorów gwiezdnych

Omówmy kilka przykładów, aby łatwo zrozumieć pojęcie wzorców w Javie.

Przykład 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();
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Przykład 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(" ");
)
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Przykład 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(" ");
)
)
)
)

Wynik:

Przykład 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();
)
)
)

Wynik:

Wniosek

Do tej pory omawialiśmy różne typy wzorców w języku programowania Java. Te wzorce są najlepszymi praktykami stosowanymi przez doświadczonych obiektowych projektantów oprogramowania. Użytkownicy mogą wykorzystywać te wzorce projektowe do dyskusji na temat projektowania oprogramowania obiektowego. Za pomocą tych wzorców niedoświadczeni programiści mogliby nauczyć się projektowania oprogramowania w łatwy i szybszy sposób.

Polecane artykuły

To był przewodnik po Star Patterns w Javie. Tutaj omawiamy wprowadzenie i różne przykłady wraz z przykładowym kodem. Możesz także przejrzeć nasze inne sugerowane artykuły, aby dowiedzieć się więcej -

  1. Co to jest dziedziczenie Java?
  2. Co to jest wzorzec projektowy w Javie?
  3. Co to jest Java Hibernacja?
  4. Programowanie aplikacji Java
  5. Wprowadzenie do wzorców gwiezdnych w PHP
  6. Wzory w C #