【问题标题】:Unreachable code in the Eclipse editorEclipse 编辑器中无法访问的代码
【发布时间】:2016-05-16 13:20:24
【问题描述】:

我对这段代码有疑问,它显示“无法访问代码”。
它说的是

EntityLivingBase entity = (EntityLivingBase) theObject;

是无法访问的代码。
这是我的代码:

@Override
public void onRender() {

    if (!this.isToggled())
        return;

    for(Object theObject : mc.theWorld.loadedEntityList) {
        if(!(theObject instanceof EntityLivingBase)) {
            continue;

            EntityLivingBase entity = (EntityLivingBase) theObject;

            if(entity instanceof EntityPlayer) {
                if(entity != mc.thePlayer)
                    player(entity);
                continue;
            }

            if (entity instanceof EntityMob) {
                mob(entity);
                continue;
            }

            if (entity instanceof EntityAnimal) {
                animal(entity);
                continue;
            }

            passive(entity);
        }
    }
super.onRender();
}

【问题讨论】:

  • 您可以删除所有这些 continue 语句,我怀疑您的逻辑在初始 if 语句中是错误的。 !(theObject instanceof EntityLivingBase) 应该是 theObject instanceof EntityLivingBase,因为您随后会立即将其转换为该值。不要称它为theObject。称它为比这更有意义的东西:)
  • 谢谢,这对我有用!现在它正在工作。 〜我对Java脚本并不是很熟悉,我还在学习。谢谢!
  • @LiamYS 请了解javajavascript 之间的区别(除了相似的名称和类似algol 的语法,它们非常不同)。

标签: javascript java eclipse


【解决方案1】:

使用continue,您将在此时结束迭代。其余代码无法访问,因为它永远不会被处理。

【讨论】:

  • 所以我需要删除继续; ?
【解决方案2】:

别人说的都是对的。 continue 总是跳过其余的代码,这变得无用,因为它永远无法到达,因此您会收到无法访问的语句错误。

我认为这是一个拼写错误,如果我理解正确,代码应该是:

if(!(theObject instanceof EntityLivingBase)) {
        continue;
} // This is missing.

其余代码应在您关闭 if 块之后。

【讨论】:

    【解决方案3】:

    continue 关键字使 Java 跳过其余代码,然后重新迭代 for 循环中的下一个元素。您的代码所做的是在 for 循环中的其余代码有时间运行之前跳到下一次迭代。我真的不知道你为什么把它放在那里,也许是对它的使用有误解?

    【讨论】:

      猜你喜欢
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多