首頁  >  文章  >  Java  >  Java程式創建金字塔和圖案

Java程式創建金字塔和圖案

WBOY
WBOY轉載
2023-09-05 15:05:10875瀏覽

如果有人想在 Java 程式語言方面打下堅實的基礎。然後,有必要了解循環的工作原理。此外,解決金字塔模式問題是增強 Java 基礎知識的最佳方法,因為它包括 for 和 while 迴圈的廣泛使用。本文旨在提供一些 Java 程序,借助 Java 中可用的不同類型的循環來列印金字塔圖案。

建立金字塔圖案的 Java 程式

我們將透過 Java 程式列印以下金字塔圖案 -

  • 倒星星金字塔

  • #星金字塔

  • 數字金字塔

讓我們一一討論。

模式 1:倒星金字塔

Java程式創建金字塔和圖案

#

方法

  • 宣告並初始化一個指定行數的整數「n」。

  • 接下來,將空間的初始計數定義為 0,將星形的初始計數定義為“n n – 1”,這樣我們就可以保持列數為奇數。

  • 建立一個巢狀的 for 循環,外部循環將運行到“n”,第一個內部 for 循環將列印空格。列印後,我們將在每次迭代時將空間計數增加 1。

  • 再次使用另一個內部 for 迴圈來列印星星。列印後,我們會將星星數減 2。

範例

public class Pyramid1 {
   public static void main(String[] args) {
      int n = 5;
      int spc = 0; // initial space count
      int str = n + n - 1; // initial star count
      // loop to print the star pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc++; // incrementing spaces
         for(int k = 1; k <= str; k++) { // stars
            System.out.print("*\t");  
         }
         str -= 2; // decrementing stars
         System.out.println();
      }
   }
}

輸出

*  *  *  *  *  *  *  *  *	
   *  *  *  *  *  *  *	
      *  *  *  *  *	
	 *  *  *	
	    *	

圖案2:星形金字塔

Java程式創建金字塔和圖案

方法

  • 宣告並初始化一個指定行數的整數「n」。

  • 建立一個巢狀的 for 循環,外部 for 迴圈將運行到“n”,內部 for 迴圈將運行到空格數並列印空格。列印後,我們會將空格數減 1。

  • 再次採用另一個內部 for 循環,該循環將運行到星星數並列印星星。列印後,我們會將星星計數增加 2。

範例

public class Pyramid2 {
   public static void main(String[] args) {
      int n = 5; // number of rows
      int spc = n-1; // initial space count
      int str = 1; // initial star count
      // loop to print the pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc--; // decrementing spaces
         for(int k = 1; k <= str; k++) { // stars
            System.out.print("*\t");  
         }
         str += 2; // incrementing stars
         System.out.println();
      }
   }
}

輸出

            *	
	 *  *  *	
      *	 *  *  *  *	
   *  *  *  *  *  *  *	
*  *  *  *  *  *  *  *  *	

模式 3:數字金字塔

Java程式創建金字塔和圖案

方法

我們將在這裡使用先前的程式碼,但我們將列印每行中的列號,而不是列印星星。

範例

public class Pyramid3 {
   public static void main(String[] args) {
      int n = 5; // number of rows
      int spc = n-1; // initial space count
      int col = 1; // initial column count
      // loop to print the pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc--; // decrementing spaces
         for(int k = 1; k <= col; k++) { // numbers
            System.out.print(k + "\t");  
         }
         col += 2; // incrementing the column
         System.out.println();
      }
   }
}

輸出

            1	
	 1  2  3	
      1	 2  3  4  5	
   1  2  3  4  5  6  7	
1  2  3  4  5  6  7  8  9

結論

在本文中,我們討論了三個列印金字塔圖案的 Java 程式。這些模式解決方案將幫助我們解碼模式問題的邏輯,並使我們能夠自己解決其他模式。為了解決此類模式,我們使用循環和條件塊。

以上是Java程式創建金字塔和圖案的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除