结果如下:
public class Test {
static class Faction {
private Integer n;
public Faction(int n) {
this.n = n;
}
public void show(){
System.out.println(n+"的阶乘是:"+factorial(n));
}
public Integer factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
}
public static void main(String[] args) {
Faction faction=new Faction(5);
faction.show();
}
该回答引用chatgpt:
public static long show(int n) {
if (n < 0) {
throw new IllegalArgumentException("输入的数必须为非负整数");
}
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
System.out.println(n + "! = " + factorial);
return factorial;
}