• October 5th, 2010, 04:42 PM
mindlessn00b
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
Code Java:

```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.
• October 5th, 2010, 04:51 PM
mindlessn00b
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?
• October 5th, 2010, 05:17 PM
Brt93yoda
Next time surround your code in [highlight=java][/highlight] tags. The reason your code is running twice is because of these two lines:
Code java:

```int steps=simulation(n); simulation(n); //Remove this line```

Here is the finished code

Code java:

```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; } }```
• October 5th, 2010, 05:29 PM
mindlessn00b