2014-10-27 78 views
-1
private byte isTargetEast(IRobot robot) { 

byte resultE = 0; 
if (robot.getLocation().x > robot.getTargetLocation().x) { 
    resultE = -1; 
} else if (robot.getLocation().x < robot.getTargetLocation().x) { 
    resultE = 1; 
} else { 
    resultE = 0; } 
return resultE; 

// returning 1 for 'yes', -1 for 'no' and 0 for 'same latitude'. 



} 

給出錯誤'erro:非法表達式的開始'和'錯誤';'預計總共有6個錯誤,2個非法啓動,4個分號,全部在專用字節行上Java錯誤:不是語句if-else

+5

它看起來像你想要做的任務,而不是你有一個布爾條件。刪除&&和括號,用分號分隔每個賦值。 – 2014-10-27 22:24:29

+0

你沒有對if/else body的布爾值做任何事情,那就是問題 – engineercoding 2014-10-27 22:24:58

+0

@SteBra你在說什麼循環? – f1sh 2014-10-27 22:25:18

回答

1

if條件的語法是完全錯誤的。你想做什麼?您正在混合分配和條件代碼。

你可以做分配是這樣的:

if (chanceDir >= 0.5) { 
    Dir = IRobot.AHEAD; 
    Vir = IRobot.BEHIND; 
    Hir = IRobot.AHEAD; 
    Sir = IRobot.BEHIND; 
} else { 
    Dir = IRobot.RIGHT; 
    Vir = IRobot.LEFT; 
    Hir = IRobot.LEFT; 
    Sir = IRobot.RIGHT; 
} 

if (oppchanceDir >= 0.5) { 
    Dir = IRobot.BEHIND; 
    Vir = IRobot.AHEAD; 
    Hir = IRobot.BEHIND; 
    Sir = IRobot.AHEAD; 
}else { 
    Dir = IRobot.LEFT; 
    Vir = IRobot.RIGHT; 
    Hir = IRobot.RIGHT; 
    Sir = IRobot.LEFT; 
} 

或者你也可以做檢查的條件類似的:

if (chanceDir >= 0.5 && ((Dir == IRobot.AHEAD) && (Vir == IRobot.BEHIND) && (Hir == IRobot.AHEAD) && (Sir == IRobot.BEHIND))) { 
    // do something here 

}else if((Dir == IRobot.RIGHT) && (Vir == IRobot.LEFT) && (Hir == IRobot.LEFT) && (Sir == IRobot.RIGHT)){ 
    // do something here 
}else{ 
    // do something here 
} 

if (oppchanceDir >= 0.5 && ((Dir == IRobot.BEHIND) && (Vir == IRobot.AHEAD) && (Hir == IRobot.BEHIND) && (Sir == IRobot.AHEAD))) { 
    // do something here 

} else if ((Dir == IRobot.LEFT) && (Vir == IRobot.RIGHT) && (Hir == IRobot.RIGHT) && (Sir == IRobot.LEFT)) { 
    // do something here 
}else{ 
    // do something here 
} 
+0

我試圖在一個條件下更改一組變量的值。是的布爾用法是愚蠢的:D謝謝 – necrokamios 2014-10-27 22:39:51

+0

編輯:我試過你建議的第一個任務,但它仍然與'錯誤:不是一個聲明' – necrokamios 2014-10-27 22:43:26

+0

你可以顯示你的枚舉。 erros apears在哪一行上? – Mosa 2014-10-27 22:45:09