简体   繁体   中英

Why the function is executing statements even after return statement is executed?

I checked with debugging also it is going to statement =>if(sum==1) return true; but then also it is executing further statements.

static long solve(int l, int r){
    // Your code goes here
    long sum=0;
    boolean t=0;
    for (int i=l;i<=r;i++) {
        t=beautiful(i);
        if (t==true) {
            sum=sum+i;
        }
    }
    return sum;
}

static boolean beautiful(int num) {
    if (num<=0) return false;
    if (num==1) return true;
    int sum=0;
    while (num>0) {
        int rem=num%10;
        sum=sum+rem*rem;
        num=num/10;
    }
    if (sum==1) {
        System.out.print("some");
        return true;
    }
    System.out.print("one");
    beautiful(sum);
    return false;
}

After then you call beautiful method, it goes into it and gets the result and continues. So you can do this despite you call beautiful method and return false;

return beautiful(sum);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM