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)
                 System.out.println("Bounce!\nTime: " + time + " Height" + height);
                 System.out.println("Time: " + time + " Height: " + (height-32));

Edited by baseballer: n/a

6 Years
Discussion Span
Last Post by 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


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.

Edited by NormR1: n/a


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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.