一个小问题

yjl_66 2008-03-21 04:16:53
import java.Math.BigInteger;
import java.util.*;
public class fac
{
protected static ArrayList table=new Arraylist();
static
{
table.add(BigInteger.valueOf(1));
}

public static synchronized BigInteger fac(int x)
{
if(x<0) throw new IllegalArgumentException("x must be non-negative");
for(int size=table.size();size<=x;size++)
{
BigInteger lastfact=(BigInteger)table.get(size-1);
BigInteger nextfact=lastfact.multiply(BigInteger.table.valueOf(size));
table.add(nextfact);
}
return (BigInteger)table.get(x);
}
public static void main(String[] para)
{
for(int i=0;i<=50;i++)
{
System.out.println(i+"!="+factorial(i));
}
}
}

import java.Math.BigInteger;编译时说包不存在?为什么?难道还要建一个包?这不是java提供的吗?再一个系统为什么总是把math中的M大写?
...全文
65 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohoku 2008-03-21
  • 打赏
  • 举报
回复
已经改好了:
package test;

import java.math.BigInteger;
import java.util.*;

public class Fac {
protected static ArrayList table = new ArrayList();
static {
table.add(BigInteger.valueOf(1));
}

public static synchronized BigInteger fac(int x) {
if (x < 0)
throw new IllegalArgumentException("x must be non-negative");
for (int size = table.size(); size <= x; size++) {
BigInteger lastfact = (BigInteger) table.get(size - 1);
BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
table.add(nextfact);
}
return (BigInteger) table.get(x);
}

public static void main(String[] para) {
for (int i = 0; i <= 50; i++) {
System.out.println(i + "!=" + fac(i));
}
}
}


最后建议你用eclipse 开发,免得有很多错误!
xiaolingshi 2008-03-21
  • 打赏
  • 举报
回复
程序里全是错误
1。{
protected static ArrayList table=new Arraylist();
----------------------
Arraylist()应该是 ArrayList();
2.factorial(i)函数在哪啊?
3.楼上包的写法是正确的,大写Math类在java.lang包里
gongyali2005 2008-03-21
  • 打赏
  • 举报
回复
java.math.BigInteger

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧