/* * num为0到100 即表示0%-100%
*/ public static Boolean probability(int num) {
int flag = random.nextInt(100) +1 ;
if (flag <= num) return true;
return false;
}
//递归
public static int recursion(int flag,int nature){
if(probability(flag)){
nature+=1;
System.out.println("nature="+nature);
recursion(flag,nature);
}
return nature;
}
public static void main(String[] args) {
System.out.println(recursion(50,0));
}
怎么会出现这样的运行结果
nature=1
nature=2
1
个人认为应该为
nature=1
nature=2
2
Java 递归
------解决方案--------------------
nature+=1;???
????????System.out.println("nature="+nature);???
????????recursion(flag,nature);?
=========================================
nature = recursion(flag,nature);
------解决方案--------------------
public static int recursion(int flag,int nature)
========================================
替归只是一个方法调用本身,你怎么强加意志让这个变量等于你第二次计算的值?