Java计算n的阶乘

利用Java提供的biginteger类进行Java大整数的运算

public class n的阶乘 {
  public static void main(String[] args) {
  
   //n的阶乘就是1*2*3...*n;-->把1换成sum得出公式sum=sum*1.....sum*n
   //我用的是for循环运算公式
 Scanner scanner=new Scanner(System.in);
  int n=scanner.nextInt();//要算什么数的阶乘
  int coun2=1;
  String coun="";

  BigInteger bigIntegerSUM=new BigInteger("1");//声明一个变量
  
    for (int i = 0; i <n; i++) {
    coun+=coun2++;
    /*
    由于我的水平有限不懂biginteger的自加怎么写,所以只能用一个不断增加的int变成字符串然  后再放到bigIntegerB里面去
    */
   BigInteger bigIntegerB=new BigInteger(coun);//放入biginteger
   bigIntegerSUM=bigIntegerSUM.multiply(bigIntegerB)//公式sum=sum*1.....sum*n
   coun="";//记得把coun变回空字符串
 }
  System.out.println(bigIntegerSUM);
  
    
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值