用递归的方法编一个n的阶乘的java程序

2024-12-03 11:00:28
推荐回答(1个)
回答1:

递归
public int factorial(int m)
{
if (m < 0)
return 0;
else if ( m == 1)
reteurn 1;
else if (m > 1)
return m * factorial(m-1);
}

public int factorial(int m)
{
if (m < 0)
return 0;
else if ( m == 1)
reteurn 1;
else if (m > 1)
{
int sum = 1
for (int i = 2; i <= m; i++)
sum = sum * i;
return sum;

}
}