4 Replies - 425 Views - Last Post: 06 October 2012 - 01:39 PM Rate Topic: -----

#1 freejumps  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 12

Making an int count up from 0 to 100, then back down from 100 to 0?

Posted 06 October 2012 - 01:19 PM

So, I have some h/w where we're given a program of a bouncing ball. The ball bounces around in a window hitting the edges and bouncing off, and it also gradually increases in size randomly:

if (rand.nextDouble() < 0.1) ballSize++;



Pretty simple, I guess. The ballSize is an int set at 10, and it gradually gets bigger infinitely. We're supposed to cap it to an arbitrary number, and while I can manage to make it stop increasing at a certain int, I can't figure out how to reverse the operation and have it start counting back down. All I could think of something like:

if (ballSize == 100) ballSize--;



But obviously that won't keep making it decrease. Can anyone try and lead me to the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Making an int count up from 0 to 100, then back down from 100 to 0?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: Making an int count up from 0 to 100, then back down from 100 to 0?

Posted 06 October 2012 - 01:21 PM

Think about using a boolean to trigger a decrease in size. When the ball hits size n, invert the boolean. Then based on the boolean's value, either add or subtract the changeInSize constant from the ballSize.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Making an int count up from 0 to 100, then back down from 100 to 0?

Posted 06 October 2012 - 01:27 PM

I wouldn't use a boolean. Rather, I'd use a variable with the rate of change. The variable could start out at 1 and change to -1 at some point. The main loop would always be applying += change.
Was This Post Helpful? 1
  • +
  • -

#4 freejumps  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 12

Re: Making an int count up from 0 to 100, then back down from 100 to 0?

Posted 06 October 2012 - 01:36 PM

Ahh using a boolean makes a lot of sense. Got it working perfectly now, thanks for the quick reply!
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: Making an int count up from 0 to 100, then back down from 100 to 0?

Posted 06 October 2012 - 01:39 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1