2014-03-30 85 views
-2

我對編碼非常陌生,並且不斷收到這個錯誤,我真的需要幫助。這是我的代碼:令牌上的語法錯誤`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; 
    } 

回答

2

這裏存在的條件後多餘的分號:

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

刪除它。聲明將如下所示:

if (stack.getItem() == halo.TitaniumLeggings) { ... } 
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 
} 

這很糟糕。

+0

爲什麼Java在明確沒有意義的情況下允許這是有效的語法?基於布爾條件執行空白塊?誰需要那個? – ADTC

+0

它不執行空白塊。它相當於它。無論哪種方式,都同意:這是毫無意義的。我能想到的唯一用例是條件還能做什麼,但無論如何這樣做會令人困惑。 – aliteralmind

+0

我想到了副作用的情況,但Java仍然可以顯示編譯錯誤,並且IDE可以提供快速修復「如果檢查保持副作用,則刪除」。至少Java可以使if檢查後輸入一個簡單的分號無效。允許這種毫無意義的語法是混淆的常見來源! – ADTC

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 -statements;)

相關問題