Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Stuck help please!

So I finally got the main part of my code working... have to simulate a robot taking random steps but now i cant figure out how to get the average of steps he took throughout the whole code
```public class Robot {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=20;
int steps=simulation(n);
simulation(n);						//Calls the simulation method

}
public static int steps(int n){ 	//Start of method steps
int total = 0;   				//initalizing total
for (int i=0;i<=n;i++){			//incremental for loop
int steps = (int) (5 * Math.random()) - 1;//determines the random number of steps
total = total+steps;		//adds the steps per try

}return total;					//returns the total number of steps

}
public static int simulation(int n){//Star of metod simulation
int steps=0;
for(int i=1;i<=n;i++){			//incremental for loop
System.out.println("on simulation "+i+" the robot walked " //prints out the sim number total steps and in how many tries
+steps(n)+" in "+n+" tries");
steps= steps(n);
}return steps;
}
}```
any ideas? i need to add the amount of steps taken per simulation, and im really not sure how.

2. ## Re: Stuck help please!

Okay i figured out how to do the average but i dont see why im getting the simulation loop to run twice my output is 1-20 and than 1-20 again? what am i missing?

3. ## Re: Stuck help please!

Next time surround your code in [highlight=java][/highlight] tags. The reason your code is running twice is because of these two lines:
```int steps=simulation(n);
simulation(n); //Remove this line```

Here is the finished code

```public class Robot {

public static void main(String[] args) {
int steps=simulation(20);
}

public static int steps(int n){ //Start of method steps
int total = 0; //initalizing total
for (int i=0;i<=n;i++){ //incremental for loop
int steps = (int) (5 * Math.random()) - 1;//determines the random number of steps
total = total+steps; //adds the steps per try
}
return total; //returns the total number of steps
}

public static int simulation(int n){//Star of metod simulation
int steps=0;
for(int i=1;i<=n;i++){ //incremental for loop
System.out.println("on simulation "+i+" the robot walked " //prints out the sim number total steps and in how many tries
+steps(n)+" in "+n+" tries");
steps= steps(n);
}
return steps;
}
}```

4. ## The Following User Says Thank You to Brt93yoda For This Useful Post:

mindlessn00b (October 5th, 2010)

5. ## Re: Stuck help please!

Ohhhh okay thank you very much. I didn't realize it was that simple!

6. ## Re: Stuck help please!

Don't worry, it happens to all of us.