Java 下面的代码应该在重试3次后停止,并将最终输出打印为false,但它被卡在第一个方法调用中

Java 下面的代码应该在重试3次后停止,并将最终输出打印为false,但它被卡在第一个方法调用中,java,Java,背景: 因此,下面的代码需要修复,它总是以递归模式进入,无法给出最终输出或最多重试3次 // starting of the main java program public class RecursiveCall { public static void main(String[] args) { Boolean result = callingFirstMethod(0); System.out.println("Name = "+ resul

背景: 因此,下面的代码需要修复,它总是以递归模式进入,无法给出最终输出或最多重试3次

// starting of the main java program    
public class RecursiveCall {
    public static void main(String[] args) {
        Boolean result = callingFirstMethod(0);
        System.out.println("Name = "+ result);
    }

// calling callingFirstMethod with a variable with tryCount as 0
    public static Boolean callingFirstMethod(int tryCount){
        final int maxRetries = 3;
        boolean successful = false;

// method call which fails in this method and always return false
        successful = divisonMethods();

// condition which should be tried only 3 times, need to fix that 
        while(tryCount < maxRetries && !successful){
            try {
                Thread.sleep(10000);
            } catch (RuntimeException e){
                System.out.println("Exception Occured with Timer"+ e.getMessage());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
// this is where recursion actually starts and in since it always fails and enter in the infinite loop 
//and trycount value never increases because in the first call itself it will never complete the loop and never increase the value in the trycount to 2.
            successful = callingFirstMethod( tryCount++);
        }
        return successful;
    }

// method which always returns false 
    public static Boolean divisonMethods(){
        if(5>6){
            return true;
        }else{
            return false;
        }
    }
}
//主java程序的启动
公共类递归调用{
公共静态void main(字符串[]args){
布尔结果=调用第一个方法(0);
System.out.println(“Name=“+result”);
}
//使用tryCount为0的变量调用callingFirstMethod
公共静态布尔调用FirstMethod(int-tryCount){
最终int maxRetries=3;
布尔成功=假;
//方法调用,该调用在此方法中失败并始终返回false
成功=除数方法();
//只需尝试3次的情况,需要修复
while(tryCount6){
返回true;
}否则{
返回false;
}
}
}
背景:
因此,下面的代码需要修复,它总是以递归模式进入,无法给出最终输出或最多重试3次

我认为您不理解递归,在这种情况下,最好避免递归并首先收集一些经验。证明了每个递归都可以写成循环,反之亦然

// starting of the main java program    
public class RecursiveCall {
    public static void main(String[] args) {
        Boolean result = callingFirstMethod(0);
        System.out.println("Name = "+ result);
    }

// calling callingFirstMethod with a variable with tryCount as 0
    public static Boolean callingFirstMethod(int tryCount){
        final int maxRetries = 3;
        boolean successful = false;

// method call which fails in this method and always return false
        successful = divisonMethods();

// condition which should be tried only 3 times, need to fix that 
        while(tryCount < maxRetries && !successful){
            try {
                Thread.sleep(10000);
            } catch (RuntimeException e){
                System.out.println("Exception Occured with Timer"+ e.getMessage());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
// this is where recursion actually starts and in since it always fails and enter in the infinite loop 
//and trycount value never increases because in the first call itself it will never complete the loop and never increase the value in the trycount to 2.
            successful = callingFirstMethod( tryCount++);
        }
        return successful;
    }

// method which always returns false 
    public static Boolean divisonMethods(){
        if(5>6){
            return true;
        }else{
            return false;
        }
    }
}
因此,在您的情况下,我们可以这样做:

// starting of the main java program    
public class RecursiveCall {
    public static void main(String[] args) {
        Boolean result = callingFirstMethod(0);
        System.out.println("Name = "+ result);
    }

// calling callingFirstMethod with a variable with tryCount as 0
    public static Boolean callingFirstMethod(int tryCount){
        final int maxRetries = 3;
        boolean successful = false;

// method call which fails in this method and always return false
        successful = divisonMethods();

// condition which should be tried only 3 times, need to fix that 
        while(tryCount < maxRetries && !successful){
            try {
                Thread.sleep(10000);
            } catch (RuntimeException e){
                System.out.println("Exception Occured with Timer"+ e.getMessage());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
// this is where recursion actually starts and in since it always fails and enter in the infinite loop 
//and trycount value never increases because in the first call itself it will never complete the loop and never increase the value in the trycount to 2.
            successful = divisonMethods();
            tryCount++;
        }
        return successful;
    }

// method which always returns false 
    public static Boolean divisonMethods(){
        if(5>6){
            return true;
        }else{
            return false;
        }
    }
}
//主java程序的启动
公共类递归调用{
公共静态void main(字符串[]args){
布尔结果=调用第一个方法(0);
System.out.println(“Name=“+result”);
}
//使用tryCount为0的变量调用callingFirstMethod
公共静态布尔调用FirstMethod(int-tryCount){
最终int maxRetries=3;
布尔成功=假;
//方法调用,该调用在此方法中失败并始终返回false
成功=除数方法();
//只需尝试3次的情况,需要修复
while(tryCount6){
返回true;
}否则{
返回false;
}
}
}
虽然如果你坚持递归,你可以。递归还允许您避免while循环

// starting of the main java program    
public class RecursiveCall {
    public static void main(String[] args) {
        Boolean result = callingFirstMethod(0);
        System.out.println("Name = "+ result);
    }

// calling callingFirstMethod with a variable with tryCount as 0
    public static Boolean callingFirstMethod(int tryCount){
        final int maxRetries = 3;
        boolean successful = false;

// method call which fails in this method and always return false
        successful = divisonMethods();

// condition which should be tried only 3 times, need to fix that 
       if (tryCount < maxRetries && !successful){            
            successful = callingFirstMethod( tryCount + 1);
        }
        return successful;
    }

// method which always returns false 
    public static Boolean divisonMethods(){
        if(5>6){
            return true;
        }else{
            return false;
        }
    }
}
//主java程序的启动
公共类递归调用{
公共静态void main(字符串[]args){
布尔结果=调用第一个方法(0);
System.out.println(“Name=“+result”);
}
//使用tryCount为0的变量调用callingFirstMethod
公共静态布尔调用FirstMethod(int-tryCount){
最终int maxRetries=3;
布尔成功=假;
//方法调用,该调用在此方法中失败并始终返回false
成功=除数方法();
//只需尝试3次的情况,需要修复
如果(tryCount6){
返回true;
}否则{
返回false;
}
}
}


使用
someVariable++
时要小心,它将首先使用该变量,然后将其递增1。除非您100%确定它是如何工作的,否则仅将其作为独立命令使用,以将某个值增加+1。不要在复杂表达式中使用它。

每当程序输入callingFirstMethod()时,tryCount等于0。然后启动一个while循环,该循环在tryCount等于maxRetries时结束。但是tryCount中的值总是小于3。。。