JVVA求阶层

来源:百度知道 编辑:UC知道 时间:2024/09/28 06:26:08
public class Fortest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(" ForTest----- " + FoTest(10));}
public static int FoTest(int m) {
if(m==0&&m==1){
return 1;
}
int s=1,i=1;
for(i=1;i<=m;i++)
{s=s*i;
return s;
}

}
}

总是说这句出问题,求解

1、if(m==0&&m==1)应该改为if(m==0||m==1),否则这个判断永远为false
2、return语句应该写在for循环外面,否则第一次计算后就返回了,因此返回值永远是1
3、现有的程序在ForTest函数最后没有返回语句,因此会报错
4、补充一句,要学会看错误内容

用递归来做

public static void main(String args[]) {
System.out.println(" ForTest----- " + FoTest(10));
}

public static int FoTest(int m) {
int result = 1;
if (m == 0) {
return 1;
} else {
result = FoTest(m - 1)*m;
}
return result;
}