Note that there are some explanatory texts on larger screens.

plurals
  1. POShortening if statement with repetition
    primarykey
    data
    text
    <p>Below are some if statements for passing the pilot of a robot the correct directions based on where he in on a map and what direction he's facing. There a bit if repetition, but when i tried to combine conditionals the program stopped working. Any tips for shortening this?</p> <pre><code> int changeX= x-lowest.getX(); int changeY= y-lowest.getY(); if(changeX!=0){ System.out.println("x changed"); if(changeX&lt;0 &amp;&amp; robot.getFacing()== 'N'){ robot.checkDirection('L','W'); } else if(changeX&lt;0 &amp;&amp; robot.getFacing()== 'W'){ robot.checkDirection('L','W'); }else if(changeX&lt;0 &amp;&amp; robot.getFacing()== 'S'){ robot.checkDirection('R','W'); }else if(changeX&lt;0 &amp;&amp; robot.getFacing()== 'E'){ robot.checkDirection('R','W'); }else if (changeX&gt;0 &amp;&amp; robot.getFacing()== 'S'){ robot.checkDirection('L','E'); }else if (changeX&gt;0 &amp;&amp; robot.getFacing()== 'E'){ robot.checkDirection('L','E'); }else{ robot.checkDirection('R', 'E'); } }else{ System.out.println("y changed"); if(changeY&lt;0 &amp;&amp; robot.getFacing()== 'N'){ robot.checkDirection('L','S'); }else if(changeY&lt;0 &amp;&amp; robot.getFacing()== 'W'){ robot.checkDirection('L','S'); }else if(changeY&lt;0 &amp;&amp; robot.getFacing()== 'E'){ robot.checkDirection('R','S'); }else if(changeY&lt;0 &amp;&amp; robot.getFacing()== 'S'){ robot.checkDirection('R','S'); }else if (changeY&gt;0 &amp;&amp; robot.getFacing()== 'S'){ robot.checkDirection('L','N'); }else if (changeY&gt;0 &amp;&amp; robot.getFacing()== 'E'){ robot.checkDirection('L','N'); }else{ robot.checkDirection('R','N'); } } //change the current cell and the x/y currentCell = lowest; x=lowest.getX(); y=lowest.getY(); if(goal()){ moving=false; System.out.println("Goal Achieved!"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload