在本文中,我们将了解使用 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中文网其他相关文章!