利用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);
}
}