Syntax error on token `else` [closed]

落爺英雄遲暮 提交于 2019-12-13 09:33:00

问题


I am very new to coding and I keep getting this error I really need help with it. This is my code:

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

回答1:


Here there is an extra semicolon after the condition:

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

Remove it. Statement will be like this:

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



回答2:


Change

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

to

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

This is bad, because

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

is equivalent to

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
}

And that is bad.




回答3:


There is a ; at a wrong place IMHO

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

Should work. Don't put ; at if-statements ;)



来源:https://stackoverflow.com/questions/22746317/syntax-error-on-token-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!