Java循环编译错误 [英] Java loops compile errors

查看:99
本文介绍了Java循环编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释吗?首先,我知道为什么这样的代码

Can someone explain this to me? First of all, I know why this code

String getName(){
    for(;;){}
}

将违反return类型的方法:它是无限的,但是为什么此代码需要最终返回值?

will violate a return type method: it's infinite, but why does this code need a final return value?

String getName(){
    for(;i < limit; i++){// i is already defined
        if(someArrayList.get(i).isDead)
            continue;
        return someArrayList.get(i).name;
    }
    //needs a final return
}

返回值存在于循环内,并返回getName()方法的值,那么原因是什么?另一条帖子建议如果循环对我的条件是否定的,那么我将其重写为:

The return value exists inside the loop and returns the value for the getName() method, so what is the reason? another post suggested that what if the loop is negative for my condition, so I rewrote it this way:

String getName(){
    for(; i < 10; i++){// i is already defined
        if((i+1) == limit){
            if(!someArrayList.get(i).isDead)
                return "";
        }else{
            if(someArrayList.get(i).isDead)
                continue;
            return someArrayList.get(i).name;
        }
    }
    // needs a final return
}

即使在for循环条件下将i重新定义为0,也存在相同的编译时错误,因此我的i变为零,并且如果内部条件检查为负,我自己处理.总而言之,如果我将其嵌套在无限循环中,就可以了.

The same compile time error, even if I re-define i in the for loop condition, to 0, so my i becomes zero and if the inner condition checks out negative I handle it myself. All in all, if I nest it in an infinite loop, it's ok.

String getName(){
    for(;;){
        for(; i < limit; i++){// i is already defined
            if(someArrayList.get(i).isDead)
                continue;
            return someArrayList.get(i).name;
        }
    }
}

是因为它有范围吗?因为我觉得它涵盖了所有情况,所以我只想知道为什么,然后再添加最后一个return和生活的美好

Is it because it has a range? Because I feel it covers all scenarios, I just want to know why, before I add the last returnand life's good

最后这可以正常工作:

String getName(){
    for(;;){
        for(; i < limit; i++){// i is already defined
            if(someArrayList.get(i).isDead)
                continue;
            return someArrayList.get(i).name;
        }
        return "";
    }
}

先生. skeet说这种类型的代码在Java中是可以的

推荐答案

您需要一个最终的return语句,因为您的列表可能仅包含'dead'条目,在这种情况下,您的程序将达到'limit'并退出for循环.

You need a final return statement because your list may consist only of 'dead' entries, in which case your program will reach the 'limit' and exit the for loop.

我在这里看到评论,因此需要澄清.编译器只是不能分析所有可能的结果,并以100%的确定性确定循环是否将达到其自然终点.从理论上讲这是不可能的(有关更多信息,您可以阅读停顿问题).

I see the comments here, so a clarification is needed. The compiler simply cannot analyze all potential outcomes and decide with 100% certainty whether the loop will reach its natural end or not. It's theoretically impossible (you can read about The Halting Problem for more information).

虽然编译器有时可以弄清楚,但通常仅在相对简单的情况下才尝试这样做.其背后的原因很简单-如果您的代码足够复杂,以至于不清楚循环是否结束-它将使无法理解为什么循环后没有return语句的人类读者感到困惑.这样的代码是错误的代码.

While the compiler can figure it out sometimes, it usually only attempts to do that in relatively simple cases. The reason behind it is simple - if your code is complex enough so that it's unclear whether the loop ends or not - it'll confuse a human reader who won't understand why there's no return statement after the loop. Such code is bad code.

这篇关于Java循环编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆