Java递归大概意思就是自己调用自己。
但我们尽量少用递归,因为他不好实际操作从而把系统玩崩。他所占用的是栈内存,如果栈内存溢出会导致系统崩坏。
以下是我写的试题。写出5的乘阶。运用了递归和for循环。
public class Dg { public static void main(String[] args) { System.out.println( a(5)); } public static int a(int b){ if(b==1){ return 1; } return b*a(b-1); } } class C { public static void main(String[] args) { int a=1; for(int b=1;b<=5;b++){ a=a*b; } System.out.println(a); } }