Basically, I am trying to solve a problem a textbook. The book requires me to use karel J robot to develop programming principles. So the problem is correctly the following code so that the robot will do this: attached

This is the code I am suppose to fix. Currently the robot moves in a straight direction and doesn't turn. I do not know why. Please give me some hints, any help could be appreciated.

import cs1Lib.karel.Robot;
import cs1Lib.karel.World;
 
public class PlantSquare extends Object
{ 
   public static void main(String[] args)
   {  World world = new World();
    
      Robot karel = new Robot(12);
      world.addRobot(karel, 1, 1, World.EAST);

      int side = 0;
      while (side < 4)
      {  


         int step = 0;
         while (step < 3)
         {  
                   
           karel.move(); 
           karel.putBeeper();

         }
             karel.turnLeft();
      }
      karel.turnOff();

   }
}

Recommended Answers

All 2 Replies

Your while loops don't have any end to them. The first one would execute the second one, and since the second loop never ends, the code

karel.turnLeft()

never gets used. Try making the loops end so the other code can start.

I think its because you never increase the step integer so karel never stops the karel.move(); and karel.putBeeper(); routine. If the move(); method doesnt increase the step counter, then karel will never exit. Put something like step = step + 1; in the while loop to see if that fixes your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.