Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to get a for loop to stop with break/return

  1. #1
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default How to get a for loop to stop with break/return

    Hi! I had such success last time that instead of beating my head against my keyboard and yelling profanity at my chinchillas, I decided to see if anyone can guide me through this.

    Ok. The project is: Based on an initial (x, y) position and initial velocity (vx, vy) of an object given by the user, calculate and draw the position of a falling object before it hits the ground.

    So far I have everything working according to the rubric except my ball keeps being drawn below my ground. I've tried numerous things to end the for loop but am not having any luck. Here is what I have:


    Java Code:
    public static void drawBall( int x, int y, int vx, int vy, Graphics g, Color c )
      {
       // time step calc
        for( double i = 0; i < 100 ; i = i + .3)
        {
          
        // next X = Initial Position X + Initial Velocity X * Time Step
        // next Y = Initial Position Y + Initial Velocity Y * Time Step + .5 * 9.8 * Time Step * Time Step 
        
          // calc x and y
          
          double nextX = x + vx * i;
          double nextY = y + vy * i + .5 * 9.8 * i * i;
          
          //draw ball
          g.setColor( c );
          g.fillOval( (int)nextX, (int)nextY, 10, 10 );
          
          //stops ball from going below horizon
          double ballStop = ((double)HORIZON - 20.0);
          
          if ( nextY > ballStop )
          {
            return;
          }
          
          
        }

    HORIZON is a class constant of 705, and is the beginning of my ground on the drawing panel. I've tried using the equation, having HORIZON instead of ballStop in the if statement, subtracting, adding, casting everything as an int...I'm at a loss. Any guidance would help. Thanks!!
    Last edited by shaeway; 07-23-2013 at 08:05 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to get a for loop to stop with break/return

    You don't seem to be reading the responses on your earlier thread, so I shan't bother to give any advice here.
    Drawing Concentric Circles with For Loops Assignment

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: How to get a for loop to stop with break/return

    Quote Originally Posted by shaeway View Post
    Hi! I had such success last time that instead of beating my head against my keyboard and yelling profanity at my chinchillas, I decided to see if anyone can guide me through this.
    I do believe programming is a threat to your personal health. Perhaps you should consider seeking counseling to help you with your rage problems.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to get a for loop to stop with break/return

    I kept on reading 'enchiladas' ... it must be something psychological.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default Re: How to get a for loop to stop with break/return

    @ JosAH, the enchilada thing is actually quite common. Why I do not know, but you aren't the only one by any means. Thank you for having a sense of humor.

  6. #6
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default Re: How to get a for loop to stop with break/return

    Quote Originally Posted by DarrylBurke View Post
    You don't seem to be reading the responses on your earlier thread, so I shan't bother to give any advice here.
    Drawing Concentric Circles with For Loops Assignment

    db
    Not quite sure where you get that idea, since I watched them like a hawk the entire time I was finishing my last project. I thought that this was similar to other forums and when a new issue arrived, and you couldn't find anything that really answered your question via searching, you simply asked your question and the members either gave advice or guided you to an already posted thread that you didn't find yourself. I was obviously mistaken.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to get a for loop to stop with break/return

    You're drawing 'snapshots' of that ball w.r.t. a time 'i'. If nextY happens to be below ground level you still draw it ...

    kind regards,

    Jos
    shaeway likes this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to get a for loop to stop with break/return

    Quote Originally Posted by shaeway View Post
    Not quite sure where you get that idea
    If you had clicked the link I posted in this thread at #2 you would know exactly where I got that idea.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: How to get a for loop to stop with break/return

    Perhaps I can spell it out for you. Did you read the Forum guides and rules? Your code is still not posted within code tags.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default Re: How to get a for loop to stop with break/return

    Ah, Jim you are correct, I did forget that. My apologies. Thank you for pointing that out.

    And JosAH, that is exactly what I was not thinking about. Testing AFTER the ball was drawn was allowing it to proceed past the horizon. I needed to test the value before I drew the ball so that it would stop at the appropriate time.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to get a for loop to stop with break/return

    Quote Originally Posted by shaeway View Post
    And JosAH, that is exactly what I was not thinking about. Testing AFTER the ball was drawn was allowing it to proceed past the horizon. I needed to test the value before I drew the ball so that it would stop at the appropriate time.
    Don't stop drawing one step before because the ball would stop moving above the horizon.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default Re: How to get a for loop to stop with break/return

    I ended up setting my ballStop as HORIZON - 10, and my test remaining the same. My thinking behind that is anything beyond that point would draw the ball below the horizon level, since my ball has a diameter of 10. However, you bring up a very valid point.

    I'm honestly not sure if we needed it to touch the ground or not. There isn't anything in the rubric other than making sure the ball does not go past the horizon. I will double check though and run through the program several times, seeing what I come up with. I may need to add an extra drawing command that draws the last ball at the set y-coordinate with a new calculated x-coordinate.

  13. #13
    shaeway is offline Member
    Join Date
    Jul 2013
    Location
    Texas
    Posts
    13
    Rep Power
    0

    Default Re: How to get a for loop to stop with break/return

    Java Code:
    // draws the ball in various positions
      public static void drawBall( int x, int y, int vx, int vy, Graphics g, Color c )
      {
       // time step calc
        for( double i = 0; i < 100 ; i = i + .3)
        {
          
        // next X = Initial Position X + Initial Velocity X * Time Step
        // next Y = Initial Position Y + Initial Velocity Y * Time Step + .5 * 9.8 * Time Step * Time Step 
        
          // calc x and y
          
          double nextX = x + vx * i;
          double nextY = y + vy * i + .5 * 9.8 * i * i;
          
           //stops ball from going below horizon
          double ballStop = ((double)HORIZON - 10);
          
          if ( nextY > ballStop )
          {
            g.setColor( c );
            g.fillOval( (int)nextX, 695, 10, 10 );
            return;
          }
          
          //draw ball
          g.setColor( c );
          g.fillOval( (int)nextX, (int)nextY, 10, 10 );
          
        }
    This is what I came up with as my final and so far it's done what I needed it to do.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to get a for loop to stop with break/return

    Note that the ball runs over a parabola shaped path; if your ball hits the horizon, the parabola hits the horizon and you should be able to find the x coordinate of that intersection point. Draw the ball at that location and you're in business. You only have to do the calculations if the ball would end up below the horizon (your original problem).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Why won't my while loop break on command?
    By juhess88 in forum New To Java
    Replies: 3
    Last Post: 11-27-2012, 05:32 PM
  2. Replies: 3
    Last Post: 11-26-2012, 12:06 PM
  3. Interrupt loop without break or continue
    By mwr1976 in forum New To Java
    Replies: 2
    Last Post: 10-12-2011, 04:43 AM
  4. break or stop for loop
    By mitra in forum New To Java
    Replies: 7
    Last Post: 09-13-2011, 12:39 AM
  5. Replies: 1
    Last Post: 03-03-2009, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •