日期:2014-05-20  浏览次数:20633 次

3/27的问题,关于BigInteger
现在在学着循环语句了.在求阶乘的问题上遇到了个问题,求10的阶乘用Int和long都够用,但是求50,.100的阶乘就不够了,我就找到了一个BigInteger能够给无限大,但是我不知道怎么用.查了API也弄不懂,BigInteger不是和Int一样的吗?直接对一个数进行声明.


import java.io.*;
import java.util.Scanner;
public class T1
{
 public static void main(String[] args)
 {
  BigInteger jc=1;
  int i=1;
  int a;
  System.out.println("请输入要求阶乘的数:");
  Scanner sc=new Scanner(System.in);
  a=sc.nextInt();
  while(i<=a)
  {
   jc*=i;
   i++;
  }
  System.out.println("输入的数的阶乘是:"+jc);
 }
}
java

------解决方案--------------------

public class Test
{
public static void main(String[] args)
{

BigInteger jc = BigInteger.valueOf(1);
int i = 1;
int a;
System.out.println("请输入要求阶乘的数:");
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
while (i <= a)
{
jc = jc.multiply(BigInteger.valueOf(i));
i++;
}
System.out.println("输入的数的阶乘是:" + jc);

}
}