关于#java#的问题,请各位专家解答!

img


帮忙看一下这个编程怎么编啊!(我是非专业Java学生)希望有专家能帮我解答一下,谢谢啦!

结果如下:

img


img


代码如下:
参数自行代入一下就好了。

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;
    }