java递归思想
2021-03-03 04:30
标签:scan ann 死循环 return imp class user public ack 递归就是方法自己调用自己 eg: java递归思想 标签:scan ann 死循环 return imp class user public ack 原文地址:https://www.cnblogs.com/ylq167/p/14398737.htmlJava递归
递归构成
用递归实现阶乘package method;
import java.util.Scanner;
public class Recoursion {
public static void main(String[] args) {
System.out.println("请输入整数,以计算factorial:");
Scanner userInput = new Scanner(System.in);
int intValue = 1;
if (userInput.hasNextInt()) {
intValue = userInput.nextInt();
}
System.out.println(factorial(intValue));
userInput.close();
}
public static int factorial (int n){
if(n==1){
return 1;
}
else return n*factorial(n-1);
}
}