令牌“else”上的语法错误

时间:2014-03-30 16:10:35

标签: java syntax-error

我对编码很新,我不断收到此错误,我真的需要帮助。这是我的代码:

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; 
    }

3 个答案:

答案 0 :(得分:5)

更改

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
}

这很糟糕。

答案 1 :(得分:3)

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

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 - 陈述;)

答案 2 :(得分:2)

条件之后还有一个额外的分号:

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

删除它。声明将是这样的:

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