// Scott DeRuiter 10/15/2002
// RandomWalk.java
// A '^', that takes a random walk down the screen until it falls off on the
// left or right side.
public class RandomWalk
{
private int position;
private int move;
public RandomWalk ( )
{
position = 35;
move = 0;
}
public static void main ( String [] args )
{
RandomWalk dothewalk = new RandomWalk ( );
dothewalk.startLoop ( );
}
public void startLoop ( )
{
while ( position >= 5 && position <= 65 )
{
if ( position == 5 )
move = 1;
else if ( position == 65 )
move = -1;
else
move = (int)(Math.random ( ) * 3) - 1;
position += move;
for ( int counter = 0; counter < position; counter++ )
System.out.print ( " " );
System.out.println ( "^" );
}
}
}
Back to Lesson 8 Examples
Back to Java Main Page