import java.lang.*;
public class j019
{
int obj=100;
public static void main(String[] args)
{
fac obj=new fac();
System.out.println("100阶乘="+obj.Fac(100));
}
}
class fac
{
public static long Fac(int x)
{
if(x<0)
{
throw new IllegalArgumentException("x must be > =0");
}
if(x>=2)
{
return x*Fac(x-1);
}
else
{
return 1;
}
}
}这是我编的一个100的阶乘的小程序,经过反复修改,终于编译通过了,但运行结果却是0,不知为什么?请朋友多多指点,在这儿多谢了!
public class j019
{
int obj=100;
public static void main(String[] args)
{
fac obj=new fac();
System.out.println("100阶乘="+obj.Fac(100));
}
}
class fac
{
public static long Fac(int x)
{
if(x<0)
{
throw new IllegalArgumentException("x must be > =0");
}
if(x>=2)
{
return x*Fac(x-1);
}
else
{
return 1;
}
}
}这是我编的一个100的阶乘的小程序,经过反复修改,终于编译通过了,但运行结果却是0,不知为什么?请朋友多多指点,在这儿多谢了!
变更后 public static double Fac(int x) {
方法名首字符小写,类名首字母大写,楼主的刚好相反!
另外就是楼上所说,注意基本数据类型的表示范围if(x<0) {...}
else if(x == 1) {...}
else return x*fac(x-1);