public class CatchFish {
private static int index = 0; public static void main(String[] args) {
int howMuch = 253, num = 0; while (true) {
if (count(howMuch, num)) {
break;
} else {
howMuch += 2;
}
} } private static boolean count(int howMuch, int num) {
index++;
if (index == 5) {
System.out.println(num * 5 + 1);
return true;
}
num = howMuch * 5 + 1;
if (num % 4 == 0) {
howMuch = num / 4;
count(howMuch, num);
}
index = 0;
return false;
}
}高手来看下~
为什么count()方法到return ture;那里不退出方法,还会执行count()!

解决方案 »

  1.   

    private static boolean count(int howMuch, int num)
    {
    index++;
    if (index == 5)
    {
    System.out.println(num * 5 + 1);

    return true;

    }
    if(index==5)
    System.out.println("没有推出吗?");//这个信息从来都没有被打印出来过,说明return ture起作用了,退出了
    num = howMuch * 5 + 1;
    if (num % 4 == 0)
    {
    howMuch = num / 4;
    count(howMuch, num);
    }
    index = 0;
    return false;
    }
      

  2.   

    index = 0; 
    return false; 你每次都把index=0,所以进入了死循环,index永远=1
      

  3.   

    你这是个递归调用!~
    你conunt方法return true。返回到
    if (num % 4 == 0) { 
    howMuch = num / 4; 
    count(howMuch, num);
    }这儿来了!~
    相当于是个没设计好的递归调用。就是个死循环!~