Java程式列印星形帕斯卡三角形


在這篇文章中,我們將瞭解如何在Java中列印星形帕斯卡三角形。帕斯卡三角形是使用多個for迴圈和print語句生成的。三角形外的所有值都被認為是零(0)。第一行是0 1 0,而只有1佔據帕斯卡三角形中的一個空格,0是不可見的。第二行是透過新增(0+1)和(1+0)得到的。輸出夾在兩個零之間。這個過程持續到達到所需的級別。

問題陳述

編寫一個Java程式來列印星形帕斯卡三角形。下面是相同的演示:

輸入

Enter the number of rows in Pascal's Triangle : 8

輸出

Enter the number of rows in Pascal's Triangle : 8
The Pascal's Triangle :
        1
       1 1
      1 2 1
     1 3 3 1
    1 4 6 4 1
   1 5 10 10 5 1
  1 6 15 20 15 6 1
 1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1

使用使用者輸入

以下是列印星形帕斯卡三角形的步驟:

  • java.util包匯入Scanner類
  • 我們將定義方法並計算數字的階乘,並將計算二項式係數。
  • 建立Scanner物件以獲取使用者輸入,並將使用nextInt()獲取行數。
  • 使用巢狀迴圈生成帕斯卡三角形,以動態地格式化和計算值。

示例

在這裡,輸入是根據提示由使用者輸入的。你可以在我們的程式碼練習工具執行按鈕中即時嘗試此示例。

import java.util.Scanner;
public class PascalsTriangle {
   static int factorial(int my_input) {
      int factors;
      for(factors = 1; my_input > 1; my_input--){
         factors *= my_input;
      }
      return factors;
   }
   static int combination(int my_input,int r) {
      return factorial(my_input) / ( factorial(my_input-r) * factorial(r) );
   }
   public static void main(String args[]){
      System.out.println();
      int my_input, i, j;
      my_input = 5;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number of rows in Pascal's Triangle : ");
      my_input = my_scanner.nextInt();
      System.out.println("The Pascal's Triangle : ");
      for(i = 0; i <= my_input; i++) {
         for(j = 0; j <= my_input-i; j++){
            System.out.print(" ");
         }
        for(j = 0; j <= i; j++){
           System.out.print(" "+combination(i, j));
        }
        System.out.println();
     }
   }
}

輸出

Required packages have been imported
A reader object has been defined
Enter the number of rows in Pascal's Triangle : 8
The Pascal's Triangle :
        1
       1 1
      1 2 1
     1 3 3 1
    1 4 6 4 1
   1 5 10 10 5 1
  1 6 15 20 15 6 1
 1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1

使用預定義輸入

以下是列印星形帕斯卡三角形的步驟:

  • 定義用於計算的階乘和組合方法。
  • 直接在程式碼中設定行數。
  • 使用巢狀迴圈列印帕斯卡三角形,以使用預定義的行格式化和計算值。

示例

在這裡,整數已預先定義,其值將在控制檯上訪問和顯示:

public class PascalsTriangle {
   static int factorial(int my_input) {
      int factors;
      for(factors = 1; my_input > 1; my_input--){
         factors *= my_input;
      }
      return factors;
   }
   static int combination(int my_input,int r) {
      return factorial(my_input) / ( factorial(my_input-r) * factorial(r) );
   }
   public static void main(String args[]){
      System.out.println();
      int my_input, i, j;
      my_input = 8;
      System.out.println("The number of rows in Pascal's Triangle is defined as " +my_input);
      System.out.println("The Pascal's Triangle : ");
      for(i = 0; i <= my_input; i++) {
         for(j = 0; j <= my_input-i; j++){
            System.out.print(" ");
         }
         for(j = 0; j <= i; j++){
            System.out.print(" "+combination(i, j));
         }
         System.out.println();
      }
   }
}

輸出

The number of rows in Pascal's Triangle is defined as 8
The Pascal's Triangle :
            1
           1 1
          1 2 1
         1 3 3 1
        1 4 6 4 1
      1 5 10 10 5 1
    1 6 15 20 15 6 1
  1 7 21 35 35 21 7 1
 1 8 28 56 70 56 28 8 1

更新於:2024年9月9日

598 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.