即使代码正在输入,返回语句也不会在if块中执行

我有以下代码:

public class Persist2{

    public static int persistence(long n) {

        System.out.println("Start of execution of persistance method");

        int remainder;

        int persistValue = 1;
        int counter = 0;

        while (n > 1){
            remainder = (int)(n % 10);          
            n = n/10;           
            persistValue = persistValue * remainder;
        }

        if(persistValue < 10){
            System.out.println("persist Value is less than 10, so it must return 111");
            return 111;
        }else {
            System.out.println("Calling persist function");
            persistence(persistValue);
        }

        System.out.println("Why am i out of if else???");
        System.out.println("counter value before addition: "+counter);
        counter = 5 + counter;
        System.out.println("Counter: "+ counter);

        return 999;
    }

    public static void main(String args[]){
        long n = 39234;     
        System.out.println(persistence(n));
    }
}

输出为:

Start of execution of persistance method
Calling persist function
Start of execution of persistance method
Calling persist function
Start of execution of persistance method
Calling persist function
Start of execution of persistance method
persist Value is less than 10, so it must return 111
Why am i out of if else???
counter value before addition: 0
Counter: 5
Why am i out of if else???
counter value before addition: 0
Counter: 5
Why am i out of if else???
counter value before addition: 0
Counter: 5
999

After printing : persist Value is less than 10, so it must return 111 why does it not return 111??

为何还要执行以下部分3次?:

System.out.println("Why am i out of if else???");
System.out.println("counter value before addition: "+counter);
counter = 5 + counter;
System.out.println("Counter: "+ counter);`

以及为什么counter的值未更改为10、15。(我尝试将其设置为静态,然后更改)。

我无法理解代码流。它应该返回111或再次调用方法persistanc(n)。 应该是这样,但是在程序中的某个点,如果它进入if,然后不返回。然后移至程序结尾,并重复3次代码?? 这里发生了什么? 现在,我对持久性函数的逻辑不感兴趣,而对程序流程感兴趣。

评论