令牌上的语法错误`else`

问题描述:

我对编码非常陌生,并且不断收到这个错误,我真的需要帮助。这是我的代码:令牌上的语法错误`else`

public String getArmorTexture(ItemStack stack, Entity entity, int slot, String type){ 
    if (stack.getItem() == halo.TitaniumHelmet || stack.getItem() == halo.TitaniumChestplate || stack.getItem() == halo.TitaniumBoots) { 
     return "halo:textures/models/armor/Titanium1.png"; 
    } 
    if (stack.getItem() == halo.TitaniumLeggings); { 
     return "halo:textures/models/armor/Titanium_layar_2.png"; 
    } else { //<------ Syntax error on token "else", delete this token 
     return null; 
    } 

这里存在的条件后多余的分号:

if (stack.getItem() == halo.TitaniumLeggings); 

删除它。声明将如下所示:

if (stack.getItem() == halo.TitaniumLeggings) { ... } 

变化

if (stack.getItem() == halo.TitaniumLeggings); { 

if (stack.getItem() == halo.TitaniumLeggings) { 

这是不好的,因为

if (stack.getItem() == halo.TitaniumLeggings); { 
    //do stuff... 
} 

相当于

if (stack.getItem() == halo.TitaniumLeggings) { 
} 
    //The above EMPTY block is only executed when the 
    //if evaluates to true. The below is ALWAYS executed. 
{ 
    //do stuff 
} 

这很糟糕。

+0

为什么Java在明确没有意义的情况下允许这是有效的语法?基于布尔条件执行空白块?谁需要那个? – ADTC

+0

它不执行空白块。它相当于它。无论哪种方式,都同意:这是毫无意义的。我能想到的唯一用例是条件还能做什么,但无论如何这样做会令人困惑。 – aliteralmind

+0

我想到了副作用的情况,但Java仍然可以显示编译错误,并且IDE可以提供快速修复“如果检查保持副作用,则删除”。至少Java可以使if检查后输入一个简单的分号无效。允许这种毫无意义的语法是混淆的常见来源! – ADTC

有一个;在错误的地方恕我直言

public String getArmorTexture(ItemStack stack, Entity entity, int slot, String type){ 
    if (stack.getItem() == halo.TitaniumHelmet || stack.getItem() == halo.TitaniumChestplate || stack.getItem() == halo.TitaniumBoots) { 
     return "halo:textures/models/armor/Titanium1.png"; 
    } 
    if (stack.getItem() == halo.TitaniumLeggings) { 
     return "halo:textures/models/armor/Titanium_layar_2.png"; 
    } else { //<------ Syntax error on token "else", delete this token 
     return null; } 

应该工作。不要把;if -statements;)