首頁 > Java > java教程 > 主體

Java 中的星形圖案

WBOY
發布: 2024-08-30 16:24:43
原創
823 人瀏覽過

在本文檔中,首先,我們將了解如何使用 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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板