看看这段代码,刚写的
public class Tes
{
public static void main(String[] args)
{
System.out.println(getFactorial(5));
System.out.println(getFactorial2(5));
}
public static int getFactorial(int n)
{
if(n == 0)
return 1;
else if(n==1)
return 1;
else if(n == 2)
return 2;
else
return n*getFactorial(n-1);
}
public static int getFactorial2(int n)
{
if(n == 0)
return 1;
int val = 1;
for(int i =1; i<=n ; i++)
val *=i;
return val;
}
}
|