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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Stuck help please!

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by helloworld922; October 5th, 2010 at 09:35 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by mindlessn00b; October 5th, 2010 at 04:54 PM.

  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default 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;
    	}
    }
    Last edited by Brt93yoda; October 5th, 2010 at 05:24 PM.

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

    mindlessn00b (October 5th, 2010)

  5. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Stuck help please!

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

  6. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Stuck help please!

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

Similar Threads

  1. Stuck, looking for input
    By ptabatt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 30th, 2010, 02:54 AM
  2. I am stuck
    By hawkman4188 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 29th, 2010, 12:46 AM
  3. Stuck :(
    By hing09 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 05:20 PM
  4. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 10:21 AM
  5. stuck on this program can any one help me
    By clive in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2009, 05:54 PM