I have this project that I'm working on for my class, but I'm getting stuck with infinite loops and I'm not really sure what's wrong.
I'm supposed to assume that at time 0, the ball is at height 0 and the velocity is input by the user.
After each second the height is changed by adding the current velocity then subtracting 32.
If the height is less than zero, multiply north the height and velocity by -0.5, print "Bounce!" as well as the time and height.
And the loop is supposed to run until the ball comes to rest or bounces 5 times.
Any help would be much appreciated.

The for loop as it is now:

for(float height = velocity; height>=0; time++) 
        {
           
           if (height<=0)
             {
                 height*=-0.5;
                 velocity*=-0.5;
                 System.out.println("Bounce!\nTime: " + time + " Height" + height);
             }
            else
             {
                 System.out.println("Time: " + time + " Height: " + (height-32));
             } 
           
        }
Member Avatar for hfx642

For what you are trying to accomplish,
you should be looping time and not height.

That's what I thought but I'm not sure how to get the loop to stop after 5 bounces or velocity = 0

You could add tests for those variables' values to the if() statement ending condition.

I'm sorry I'm very new to programming, what is the format for adding a test? I tried changing up with the if, else statement but no luck yet

Currently your if statements tests if the value of height is >= 0
Make a compound condition by adding the other tests.
For example:
if( int a = 0;(a > 5) && (d <= 3) && (f > 0); a++) ...

Make a very simple program with a for loop and an assignment statement that changes the second variable and start with two conditions and some printlns to show the values as the loop goes around and when it exits.
When you get that to work with two conditions add a third and see what happens then.

Awesome, I think I get it. Thank you so much for your help!

Member Avatar for hfx642

Instead of using a for loop, use a while loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.