Hi i m trying to design a program using object oriented approach.everything works perfect for me except the bounces.If the new height is less then 0 then multiply both the height and velocity by -0.5 to simulate the bounce.

import java.util.Scanner;
public class Ball11
{
	public static void main (String [ ] args)
	{
		double height = 0;

		int times = 0;

		int speed=0;
		int bounces=0;


		int numOfBounces = 0;
		Scanner scan= new Scanner (System.in);
		System.out.println("Enter the initial velocity of the ball:");
		height=scan.nextInt();

		System.out.println("Enter the no of speed");
		speed=scan.nextInt();

		System.out.println("enter the no of bounces");
		bounces=scan.nextInt();

		System.out.println();
//Allow the user to play the ball as many  times as he wants and each time he can decide the initial ball speed and the number of bounces to stop the ball



			do
			{
				times++;
				height =height + speed;

				speed-=32;

				if (height > 0)

					System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed);
				else
				{
					height = (double) (height * (-0.5));
					speed = (int) (speed * (-0.5));
					System.out.println ("bounce!");

					System.out.println ("time = " + times + "   height = " + height + "   speed = " + speed );

					break;
				}
			} while (true);
			bounces++;




	}
}

So whats the problem with the bounces can you specify your desired output?

also you should have used your previous thread instead of creating a new one

So whats the problem with the bounces can you specify your desired output?

also you should have used your previous thread instead of creating a new one

i want my program to print the word "bounce".suppose for "enter the no bounces" if i put 5.then everytime the new height is less then zero my program will print "bounce" .after 5th bounce my progrm will stop.

Thats because you put in a break statement to stop the whole do while loop
Use a different approach in stopping the loop like a boolean

For example the loop will stop once it reaches the required amount of bounces

Thats because you put in a break statement to stop the whole do while loop
Use a different approach in stopping the loop like a boolean

but i also want my program to print "bounce".depends on nos of time user want.

but i also want my program to print "bounce".depends on nos of time user want.

I'm not contradicting you I said the loop should stop once the required number of bounces is met, meaning you can still print "bounce" and will only not print anymore once the condition is met... you can still have the else statement I only said not to use break;

I'm not contradicting you I said the loop should stop once the required number of bounces is met, meaning you can still print "bounce" and will only not print anymore once the condition is met... you can still have the else statement I only said not to use break;

i remove break from my codes and run the program.my program keeps running and doesn't stop.nomatter what no i put for "enter the no of bounces".

I just said that you should use the number of bounces as a requirement for stopping the loop

(said that in 3 posts plus this already)

use a boolean like statement

a boolean is set to either true or false
Once the number of bounces have been met then change the value of the boolean to stop the loop

pseudo:

boolean value = false;

current bounce = 0;

do{

if(current bounce == number of bounce user wants) value = true;
else current bounce is added by 1;
 //you can put the iterator outside of if else statement but position will depend if your using do while loop or while loop

}while(value = false);

Do I need to post this again?

I just said that you should use the number of bounces as a requirement for stopping the loop

(said that in 3 posts plus this already)

use a boolean like statement

how can i do that.

I gave a pseudo a while ago
if you don't know about booleans then you can learn it using google then implement it on your code

I gave a pseudo a while ago
if you don't know about booleans then you can learn it using google then implement it on your code

thanks

I really hope you learned something here :idea:

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.