Hi all,
Im having trouble with a program wheree a person starts in the middle of a 7 foot bridge. Heres the description:
Someone is standing at the center of a bridge that is 7 ft bridge long. Their stride is exactly one foot. They can’t control the direction they are going but the bridge is very narrow and they can only go forward or backward with each step.
Write a program that calculates how many steps the person will walk before exiting the bridge. Have the program execute this simulation 1000 times and as your output display the average and greatest number of steps taken. (Hint: generate a random number that is either 0 or 1 and let one equal forward and the other backward). Do this 20 times so that you can make comparative observations.
This is what I have so far:
import java.util.Random;
public class prog214a
{
public static void main (String[] args)
{
Random generator = new Random();
System.out.println("1000 iterations");
int runs=0;
int iter=1000;
int count=7/2;
int random;
System.out.println("Run\tAvarage\tGreatest Number of Steps");
for(runs=1;runs<20; runs+=1)
{
for(iter=1000;iter>1;iter-=1)
{
double avg= count/iter;
random = generator.nextInt(2);
if(random==0)
{count-=1;}
if(random==1)
{count+=1;}
if(count<=0 || count>=7)
{System.out.println("#" +runs +":\t" +avg +"\t" +count);
count =0;}
}
}
}
}
What do I need to fix? Im not sure of the problem here. A fast reply would be great because I need to have this program done today >.<
Thanks!