在本文中,我們將了解使用 Java 程式語言編寫用於階乘計算的程式碼的各種方法。作為一種易於使用的物件導向語言,Java 是一種獨立於平台的簡單程式語言。 Java 的編譯器和解釋器是以安全性為主要方面開發的。 Java 的應用範圍非常廣泛。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
階乘,符號為“!” (感嘆號),是將一個數字與所有較小的數字相乘的數學運算。例如,如果數字是 5,階乘的輸出將為 5! = 5*4*3*2*1 = 120.
1.完成您的程式碼並將其另存為 (檔案名稱).java
2.開啟終端機並執行以下 java 指令。
3.上面的指令將產生一個類別檔案。
4.現在,執行類別檔案。
以下是使用不同方法的不同範例:
接下來,我們將編寫一個簡單的階乘計算的 Java 程式。
public class Factorial { public static void main(String args[]) {int i, fact=1; int number=5; for(i=1;i<=number;i++) { fact=fact*i; } System.out.println("Factorial of "+number+" is: "+fact); } }
使用任意檔名和 .java 副檔名儲存上述程式碼。
代碼說明:
它從兩個變數「i」和「fact」開始,值為 1,然後「number」為 5,這是我們計算階乘的數字。我進入For循環,不斷增加i的值,直到我們將它與一個數字匹配,即5。在增加的同時,每次fact的值增加,它都會相乘,並且fact被分配一個新值。
輸出:
另一種常用的方法是我們要求使用者輸入數字進行計算,而不是預先定義它。
參考以下程式碼進行基於使用者輸入的計算:
import java.util.Scanner; class Facto{ public static void main(String args[]) { int q, a, fact = 1; System.out.println("Please Enter a number:"); Scanner in = new Scanner(System.in); q = in.nextInt(); if ( q < 0 ) System.out.println("Please enter a number greater than 0:"); else { for ( a = 1 ; a <= q ; a++ ) fact = fact*a; System.out.println("Factorial of "+q+" is = "+fact); } } }
像前面的範例一樣保存上面的程式碼。
代碼說明:
前面的範例和上面的範例之間的主要區別在於使用者輸入;休息也是一樣。程式碼會要求計算一個數字,那麼如果使用者輸入的數字是負數,即“-”中的減號,則會提示“請輸入大於0的數字:”,這很明顯,因為階乘不能計算計算為負數。現在,它將接受正數並繼續計算階乘,然後列印輸出,如下圖所示。
輸出:
遞歸是程式設計世界中最有用的工具之一。遞歸基本上意味著重複使用函數。也就是說,我們不必在這裡定義額外數量的變量,這意味著我們只有兩個或更少的變數。
實現遞歸的一個主要原因是能夠減少程式碼長度並優雅地降低程式的時間複雜度。遞歸方法雖然有其優點,但也有一些缺點,從長遠來看,這些缺點可能會產生重大影響。
遞迴的缺點:
參考以下程式碼:
public class FactorialExample2 { static int factorial(int n){ if (n == 1) return 1; else return(n * factorial(n-1)); } public static void main(String[] args) { System.out.println("Factorial of 5 is: "+factorial(5)); } }
像我們之前那樣儲存並編譯程式。
代碼說明:
The above code starts with a single int variable and checks if it is equal to 1; if yes, it returns one, as factorial for 1 is 1. If not equal to 1, it proceeds with the recursion function. Our int value, for example, is 5, so it’ll be like “5 * factorial(5-1)”, factorial is called here for the second time, which is another call. Then it returns again with a newer int value, which is 4, “4 * factorial(4-1)”, now it’ll be the third call to the recursion method. Now, the newer int value is 3, which means “3 * factorial(3-1)”, now it’ll be the fourth call, and the value will be 2, which means “2 * factorial(2-1)”. The int value will be one in the next recursive call, which will terminate the function here. While every call was made, its value was saved in a Stack, which is a LIFO method. So, for the final Output, the result will be “5*4*3*2*1 = 120.”
Compared to other methods, Recursion is quite difficult to understand and to implement, but if understood well and implemented wisely, it is a good tool.
Output:
It is highly recommended to use Recursion only in the case where writing an iterative code can be quite complex.
Now that we have learned various methods for implementing Factorial Calculations in Java Let’s explore a Built-in function that does the same work in a single line.
*) IntMath
Understanding the need for arithmetic operations over a value, a few functions specific to certain value types were written, we will see the Integer type value in work.
IntMath is a class for arithmetic calculations on an int value. IntMath class comes with a range of arithmetic operations, including factorial.
Syntax:
factorial (int n)
We started with an introduction to java and how to run a java program. Then we learned about Factorial Calculation and various methods, including Recursion, to accomplish it.
Towards the end, we learned about IntMath; a Java Function primarily focused on Arithmetic operations. Java is a widely-used programming language; it comes with many features; in this article, we learned about Factorial Calculations in Java, which is a tiny aspect.
以上是Java 中的階乘的詳細內容。更多資訊請關注PHP中文網其他相關文章!