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

Page 1 of 1

4 Replies - 909 Views - Last Post: 06 October 2012 - 01:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294541&amp;s=929400e1e23b584139bd1f98657c38da&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 freejumps

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 11798
• Posts: 44,322
• 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.

#3 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• 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.

#4 freejumps

Reputation: 0
• 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!

#5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11798
• Posts: 44,322
• 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

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}