Java write a program to calculate the factorial of any natur

1个回答

  • import java.util.Scanner;

    public class DiGui {

    public static void main(String[] args){

    //创建一个输入容器

    Scanner input = new Scanner(System.in);

    System.out.println("输入一个数:");

    int grade = input.nextInt();

    DiGui d = new DiGui();

    int end = d.DiGuiDiaoYong(grade);

    System.out.println("阶乘结果:"+end);

    }

    //用递归

    public static int DiGuiDiaoYong(int n){

    if(n==1)

    return n=1;

    return DiGuiDiaoYong(n-1)*n;

    }

    }