Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help on a program involving simple logic. :| Uses karel.

807599Nov 18 2006 — edited Nov 19 2006
Here is my program:
import edu.pace.Robot;
import edu.pace.World;
import javax.swing.JOptionPane;
public class Lab09
{
	public static void main(String[] args)
	{
		String worldname = JOptionPane.showInputDialog("Which world do you want?");
		Athlete temp = new Athlete(1, 1, World.EAST, 0);
		World.readWorld(worldname);
		World.setSize(10, 10);
		World.setSpeed(10);
		///////////////////////////////////////////////////////////////////////////
		int pile1 = 0;
		int pile2 = 0;
		int pile3 = 0;
		int pile4 = 0;
		int pile5 = 0;
		//////////////////////////////////////////////////////////////////////////
		
		temp.move();
		while(temp.nextToABeeper())
		{
		temp.pickBeeper();
		pile1++;
		}
		temp.move();
		while(temp.nextToABeeper())
		{
		temp.pickBeeper();
		pile2++;
		}
		for(int x = pile1; x > 0; x--)
		{
		temp.putBeeper();
		}
		temp.move();
		while(temp.nextToABeeper())
		{
		temp.pickBeeper();
		pile3++;
		}
		for(int x = pile2; x > 0; x--)
		{
		temp.putBeeper();
		}
		temp.move();
		for(int x = pile3; x > 0; x--)
		{
		temp.putBeeper();
		}
		temp.move();
		while(temp.nextToABeeper())
		{
		temp.pickBeeper();
		pile4++;
		}
		temp.move();
		for(int x = pile4; x > 0; x--)
		{
		temp.putBeeper();
		}
		temp.move();
		while(temp.nextToABeeper())
		{
		temp.pickBeeper();
		pile5++;
		}
		temp.move();
		for(int x = pile5; x > 0; x--)
		{
		temp.putBeeper();
		}
		
	}
}
Here is a screenshot of each world:
pile1 - http://xs209.xs.to/xs209/06466/pile1.png
pile2 - http://xs209.xs.to/xs209/06466/pile2.png
pile3 - http://xs209.xs.to/xs209/06466/pile3.png

The robot starts out with 0 beepers btw.

What it's supposed to do: There are three robot worlds I'm supposed to test this on - pile1, pile2, and pile3. This code works on the first one, but fails on the 2nd and 3rd worlds, which is why I'm coming here for help. The program itself is supposed to shift each beeper pile one to the right, simple enough in idea. I feel like there is something I can do to modify this code, because i'm close to getting it working, but I don't know what I should change, which is why I posted this thread. :p

Thanks for your help,
Ben Katz (10th grade student)

Message was edited by:
B_E_A_N
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 17 2006
Added on Nov 18 2006
5 comments
231 views