Java 计算10的阶乘。。100而不重复任何代码?
我可以通过使用for循环来完成,但它太长了。我必须做大约10个循环Java 计算10的阶乘。。100而不重复任何代码?,java,Java,我可以通过使用for循环来完成,但它太长了。我必须做大约10个循环 System.out.println("factorial of 10"); int fact = 1; for (int x=1;x<=10;x++) { fact=fact*x; System.out.println(fact); } System.out.println(" factorial of 20 "); double fact1 = 1; for (double y=1;
System.out.println("factorial of 10");
int fact = 1;
for (int x=1;x<=10;x++) {
fact=fact*x;
System.out.println(fact);
}
System.out.println(" factorial of 20 ");
double fact1 = 1;
for (double y=1;y<=20;y++){
fact1=fact1*y;
System.out.println(fact1);
}
System.out.println(“10的阶乘”);
int-fact=1;
对于(int x=1;x您可以使用递归方法来计算阶乘。还可以根据您在解决方案中调整类型。如biginger
或Long
public class Factorial {
public static void main (String[] args) {
int theNum, theFact;
System.out.println("This program computes the factorial " +
"of a number.");
System.out.print("Enter a number: ");
theNum=Console.in.readInt();
theFact = fact(theNum);
System.out.println(theNum + "! = " + theFact + ".");
}
static int fact(int n) {
if (n <= 1) {
return 1;
}
else {
return n * fact(n-1);
}
}
}
公共类阶乘{
公共静态void main(字符串[]args){
在theNum,offact;
System.out.println(“此程序计算阶乘”+
“指一个数字。”);
System.out.print(“输入一个数字:”);
theNum=Console.in.readInt();
theFact=事实(theNum);
System.out.println(theNum+“!=”+offact+”);
}
静态整数事实(整数n){
if(n您可以使用递归方法计算阶乘。也可以根据您在解决方案中调整类型。如biginger
或Long
public class Factorial {
public static void main (String[] args) {
int theNum, theFact;
System.out.println("This program computes the factorial " +
"of a number.");
System.out.print("Enter a number: ");
theNum=Console.in.readInt();
theFact = fact(theNum);
System.out.println(theNum + "! = " + theFact + ".");
}
static int fact(int n) {
if (n <= 1) {
return 1;
}
else {
return n * fact(n-1);
}
}
}
公共类阶乘{
公共静态void main(字符串[]args){
在theNum,offact;
System.out.println(“此程序计算阶乘”+
“指一个数字。”);
System.out.print(“输入一个数字:”);
theNum=Console.in.readInt();
theFact=事实(theNum);
System.out.println(theNum+“!=”+offact+”);
}
静态整数事实(整数n){
如果(n您可以将每个计算出的阶乘放到一个数组中,并通过检索数组的最后一个元素来计算新的阶乘值。但是,正如您所知,您可以使用int类型最多计算12!,因为使用13!,数字将大于int的最大值(2^31-1)。您可以使用biginger对象作为解决方案
或者,如果您只需要计算这些特定数字的阶乘值,您可以调用一个递归函数,其工作原理与上述解决方案类似。您可以将每个计算出的阶乘放入一个数组,并通过检索数组的最后一个元素来计算新的阶乘值。但是,正如您所知,您可以使用int类型计算多达12!的阶乘值,因为h 13!时,数字将大于int的最大值(2^31-1)。您可以使用BigInteger对象作为解决方案
或者,如果您只需要计算这些特定数字的阶乘值,您可以调用一个递归函数,其工作原理与上述解决方案类似。用于Gamma函数
你可以用这个等式来做这个把戏。如果没有循环,它就不是什么计算机语言了
编辑:或者您可以使用以下代码,而不在任何地方使用显式循环(借用)
导入javax.swing.Timer;
导入java.awt.event.*;
导入java.util.concurrent.ArrayBlockingQueue;
公共类Fac{
公共静态整数fac(最终整数){
最终ArrayBlockingQueue队列=新的ArrayBlockingQueue(1);
最终计时器=新计时器(0,空);
timer.addActionListener(新ActionListener(){
int结果=1;
int n=_n;
已执行的公共无效操作(操作事件e){
结果*=n;
n--;
如果(n==0){
试一试{
queue.put(结果);
}捕获(例外情况除外){
}
timer.stop();
}
}
});
timer.start();
int结果=0;
试一试{
结果=queue.take();
}捕获(例外情况除外){
}
返回结果;
}
公共静态void main(字符串[]args){
系统输出打印LN(fac(10));
}
}
用于Gamma函数
你可以用这个等式来做这个把戏。如果没有循环,它就不是什么计算机语言了
编辑:或者您可以使用以下代码,而不在任何地方使用显式循环(借用)
导入javax.swing.Timer;
导入java.awt.event.*;
导入java.util.concurrent.ArrayBlockingQueue;
公共类Fac{
公共静态整数fac(最终整数){
最终ArrayBlockingQueue队列=新的ArrayBlockingQueue(1);
最终计时器=新计时器(0,空);
timer.addActionListener(新ActionListener(){
int结果=1;
int n=_n;
已执行的公共无效操作(操作事件e){
结果*=n;
n--;
如果(n==0){
试一试{
queue.put(结果);
}捕获(例外情况除外){
}
timer.stop();
}
}
});
timer.start();
int结果=0;
试一试{
结果=queue.take();
}捕获(例外情况除外){
}
返回结果;
}
公共静态void main(字符串[]args){
系统输出打印LN(fac(10));
}
}
fact
的返回值应该是一个BigInteger
,OP希望能够计算100!,这不适合int
@amit谢谢,我会在回答中记下fact
的返回值应该是BigInteger
,OP希望能够计算100!,这不需要t fit in aint
@amit谢谢,我会在回答中记下这一点