Java错误:不是声明if-else

时间:2014-10-27 22:18:51

标签: java

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个分子,全部在专用字节行

1 个答案:

答案 0 :(得分:1)

if-condition的语法完全错误。你想做什么?您正在混合分配和条件代码。

您可以像这样分配:

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
}