简体   繁体   English

为什么 function 执行完 return 语句后还在执行语句?

[英]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;我也检查了调试它会声明 =>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;因此,尽管您调用了beautiful方法并返回 false,您仍然可以执行此操作;

return beautiful(sum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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