使用遞迴查詢兩個數字的乘積的 Java 程式


在本文中,我們將瞭解如何在 Java 中使用遞迴查詢兩個數字的乘積。遞迴函式是指多次呼叫自身直到滿足特定條件的函式。

遞迴 是以自相似的方式重複專案的過程。在程式語言中,如果程式允許您在同一函式內呼叫函式,則稱為該函式的遞迴呼叫。

許多程式語言透過棧來實現遞迴。通常,每當一個函式(呼叫方)呼叫另一個函式(被呼叫方)或自身作為被呼叫方時,呼叫方函式都會將執行控制權轉移到被呼叫方。此轉移過程可能還涉及將一些資料從呼叫方傳遞到被呼叫方。

問題陳述

編寫一個 Java 程式,使用遞迴查詢兩個數字的乘積。以下是相同的演示 -

輸入

Enter two number : 12 and 9

輸出

The product of 12 and 9 is 108

不同的方法

以下是使用遞迴查詢兩個數字乘積的不同方法 -

使用使用者輸入

以下是使用使用者輸入使用遞迴查詢兩個數字乘積的步驟 -

  • Scanner 類 匯入 java.util 包 以讀取使用者輸入。
  • 在 **ProductRecursion 類** 中,建立使用者將輸入兩個數字的主方法。
  • 定義一個 Scanner 物件 (my_scanner) 以從控制檯讀取輸入。
  • 提示使用者輸入兩個數字 (my_input_1 和 my_input_2),並將這些數字儲存在相應的變數中。
  • 呼叫 getproduct 方法,將 my_input_1 和 my_input_2 作為引數傳遞。
  • 定義遞迴方法 **getproduct**
    • 如果 **my_input_1** 小於 **my_input_2**,則透過使用交換後的引數再次呼叫 getproduct 來交換引數。
    • 如果 **my_input_2** 不為零,則將 my_input_1 新增到 getproduct(my_input_1, my_input_2 - 1) 的結果中。
    • 如果 **my_input_2** 為零,則返回 0(基本情況)。
  • 在控制檯上列印這兩個數字的乘積。
  • 關閉 Scanner 物件以防止資源洩漏。

示例

在這裡,使用者根據提示輸入輸入

import java.util.Scanner;
public class ProductRecursion{
   public static void main (String[] args){
      int my_input_1, my_input_2;
      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 : ");
      my_input_1 = my_scanner.nextInt();
      System.out.print("Enter the number : ");
      my_input_2 = my_scanner.nextInt();
      System.out.println("The product of "+my_input_1 +" and " +my_input_2 +" is " +getproduct(my_input_1, my_input_2));
   }
   static int getproduct(int my_input_1, int my_input_2){
      if (my_input_1 < my_input_2)
         return getproduct(my_input_2, my_input_1);
      else if (my_input_2 != 0)
          return (my_input_1 + getproduct(my_input_1, my_input_2 - 1));
      else
         return 0;
   }
}

輸出

Required packages have been imported
A reader object has been defined
Enter the number : 12
Enter the number : 9
The product of 12 and 9 is 108

使用預定義值

以下是使用遞迴預定義值查詢兩個數字乘積的步驟 -

  • 在 **ProductRecursion 類** 中,定義主方法以計算預定義數字的乘積。
  • 為 **my_input_1** 和 **my_input_2** 設定預定義值。
  • 列印一條訊息,顯示這兩個定義的數字。
  • 呼叫 getproduct 方法,將 **my_input_1** 和 **my_input_2** 作為引數傳遞。
  • 定義遞迴方法 getproduct
    • 檢查 **my_input_1** 是否小於 **my_input_2**。如果是,則使用交換後的引數呼叫 getproduct。
    • 如果 **my_input_2** 不為零,則遞迴地將 **my_input_1** 新增到 getproduct(my_input_1, my_input_2 - 1) 的結果中。
    • 如果 **my_input_2** 為零,則返回 0 作為基本情況。
  • 在控制檯上列印這兩個數字的乘積。

示例

在這裡,整數先前已定義,並且其值在控制檯上被訪問和顯示 -

public class ProductRecursion{
   public static void main (String[] args){
      int my_input_1, my_input_2;
      my_input_1 = 12;
      my_input_2 = 9;
      System.out.println("The two numbers are defined as " +my_input_1 +" and " +my_input_2);
      System.out.println("The product of "+my_input_1 +" and " +my_input_2 +" is " +getproduct(my_input_1, my_input_2));
   }
   static int getproduct(int my_input_1, int my_input_2){
      if (my_input_1 < my_input_2)
         return getproduct(my_input_2, my_input_1);
      else if (my_input_2 != 0)
         return (my_input_1 + getproduct(my_input_1, my_input_2 - 1));
      else
         return 0;
   }
}

輸出

The two numbers are defined as 12 and 9
The product of 12 and 9 is 108

更新於:2024年11月13日

557 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告