JAVA编写,写在纸上拍给我最好。求助
2个回答
展开全部
import java.math.BigDecimal;
import java.util.Scanner;
public class Factorial3 {
public static BigDecimal factorial(BigDecimal n){
BigDecimal bd1 = new BigDecimal(1);//BigDecimal类型的1
BigDecimal bd2 = new BigDecimal(2);//BigDecimal类型的2</span><span>
BigDecimal result = bd1;//结果集,初值取1
while(n.compareTo(bd1) > 0){//参数大于1,进入循环
result = result.multiply(n.multiply(n.subtract(bd1)));//实现result*(n*(n-1))
n = n.subtract(bd2);//n-2后继续
}
return result;
}
public static void main(String[] arguments){
Scanner sc = new Scanner(System.in);
BigDecimal n = sc.nextBigDecimal();
System.out.print(n + "!=" + factorial(n));
}
}
import java.util.Scanner;
public class Factorial3 {
public static BigDecimal factorial(BigDecimal n){
BigDecimal bd1 = new BigDecimal(1);//BigDecimal类型的1
BigDecimal bd2 = new BigDecimal(2);//BigDecimal类型的2</span><span>
BigDecimal result = bd1;//结果集,初值取1
while(n.compareTo(bd1) > 0){//参数大于1,进入循环
result = result.multiply(n.multiply(n.subtract(bd1)));//实现result*(n*(n-1))
n = n.subtract(bd2);//n-2后继续
}
return result;
}
public static void main(String[] arguments){
Scanner sc = new Scanner(System.in);
BigDecimal n = sc.nextBigDecimal();
System.out.print(n + "!=" + factorial(n));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询