copy别人的,谁给解释一下class Int{
  int n;
}
class Fact{
  float fact1(int n1){
    int i;
    float x=1;
    for(i=1;i<=n1;i++)
       x=x*i;
    return x;
  }
  void fact2(Int n2){
    int i;
    int x=1;
    for(i=1;i<=n2.n;i++)
      x=x*i;
    n2.n=x;
  }
}
public class Check{
  public static void main(String[] args){
    Fact x=new Fact();
    System.out.println("10!="+x.fact1(10)); //计算10!
    Int n2=new Int();
    Fact Demo=new Fact();
    n2.n=15;
    Demo.fact2(n2);
    System.out.println("15!="+n2.n);//计算15!
  }
}

解决方案 »

  1.   

    谁出的纱布题,32bit的int型数据,15!肯定溢出
      

  2.   

    看错了,这次我纱布了返回void的话,需要传入一个对象,将结果保存在传入对象的成员中
      

  3.   

    感觉上面我发那个也不是将Fact方法的参数个数增加为两个啊
      

  4.   

    返回void的话,需要传入一个对象
      

  5.   


    class Fact{
        void fact (int n, long[] result) //定义计算n!的方法
        {
          if (result == null)
             return;
          int i;
          long x =1;
          for(i=1;i<n;i++){
           x=x*i;}
          long[0]=x;
        }
    }
    public class Check1{
        public static void main (String args[ ])
        {
          Fact x =new Fact( );
          long[] r10 = {0};
          long[] r15 = {0};
          x.fact(10, r10);
          x.fact(15, r15);
          System.out.println(r10[0]); //计算10!
          System.out.println(r15[0]); //计算15!
        }
    }
    其实我认为直接返回更好,你老师的意思估计是弄个传出参数,不过我感觉根本是多此一举。你原来代码的最大问题是保存阶乘的结果为啥用float,用long不是更好?