Johdatus Java-tähtikuvioihin
Tässä asiakirjassa näemme ensin, kuinka Java-ohjelmointia voidaan käyttää Star Pattern -ohjelmien kanssa. Tähtikuviot ovat yksi yleisimmistä Java-malliohjelmista, joita käytetään laajalti parantamaan loogista ajattelua ja parantamaan virtauksen hallintaa koskevia tietoja. Tähtikuvioiden näyttämiseksi Java-ohjelmoinnissa on käytettävä kahta silmukkaa tai kolme silmukkaa (ohjelmista riippuen). Ensimmäinen silmukka on ulompi silmukka ja toinen silmukka on sisäpiiri, joka näyttää vastaavasti rivejä ja sarakkeita.
Niille Java-ohjelmoijille, jotka haluavat tietää suunnittelumallista tapana parantaa kohdekeskeistä suunnittelu- ja kehityskykyään, tämä asiakirja on hyödyllinen.
Esimerkkejä tähtikuvioista
Keskustelemme muutamista esimerkeistä ymmärtääksesi Java-mallien käsitteen helposti.
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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(" ");
)
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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(" ");
)
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
johtopäätös
Toistaiseksi olemme keskustelleet erityyppisistä malleista Java-ohjelmointikielellä. Nämä mallit ovat parhaita käytänteitä, joita kokeneet ohjelmiston oliokeskeiset suunnittelijat käyttävät. Käyttäjät voivat käyttää näitä suunnittelumalleja keskusteluun olio-ohjelmistojen suunnittelusta. Näiden kuvioiden avulla kokemattomat kehittäjät voivat oppia ohjelmistosuunnittelun helposti ja nopeasti.
Suositellut artikkelit
Tämä on opas Tähtikuvioihin Java-ohjelmassa. Tässä keskustellaan johdannosta ja erilaisista esimerkeistä yhdessä näytekoodin kanssa. Voit myös käydä läpi muiden ehdotettujen artikkeleidemme saadaksesi lisätietoja -
- Mikä on Java-perintö?
- Mikä on Java-suunnittelumallit?
- Mikä on Java Hibernate?
- Java-sovelluskehitys
- Johdanto tähtikuvioihin PHP: ssä
- Kuviot C #