在java中使用递归调用必须使用if分支语句吗? 为什么不用就报错,用了就好?(新人,求前辈解答)
public static void main(String[] args){
int sun=0;
sun = fun(100);
System.out.println(sun);
} public static int fun(int temp){
if(temp==1){
return 1;
}else
return temp+fun(temp-1);
}能运行
public static void main(String[] args){
int sun=0;
sun = fun(100);
System.out.println(sun);
} public static int fun(int temp){
return temp+fun(temp-1);
}这样就不行了
public static void main(String[] args){
int sun=0;
sun = fun(100);
System.out.println(sun);
} public static int fun(int temp){
if(temp==1){
return 1;
}else
return temp+fun(temp-1);
}能运行
public static void main(String[] args){
int sun=0;
sun = fun(100);
System.out.println(sun);
} public static int fun(int temp){
return temp+fun(temp-1);
}这样就不行了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货