I'm writing a code, and to save space I'm wondering how I could do
this. There's no easy way to do this so I'll just give an example:
int blueCoins = playerBlueCoins;
int redCoins = playerRedCoins;
int greenCoins = playerGreenCoins;
//...and so on...
Is there some way for me to say "If the value of any of these ints is
greater than 99, the value is 99" without having to write it for every
single int?
Lew - 19 Aug 2007 16:43 GMT
> I'm writing a code, and to save space I'm wondering how I could do
> this. There's no easy way to do this so I'll just give an example:
[quoted text clipped - 7 lines]
> greater than 99, the value is 99" without having to write it for every
> single int?
Build it up pairwise with Math.min().
If you're looking for a comparison amongst all the ints, you are perforce
going to examine every int. There is not a way to circumvent physics.

Signature
Lew
Lew - 19 Aug 2007 16:48 GMT
> I'm writing a code, and to save space I'm wondering how I could do
> this. There's no easy way to do this so I'll just give an example:
[quoted text clipped - 7 lines]
> greater than 99, the value is 99" without having to write it for every
> single int?
Ultimately there is no way to find the min() of a set of comparable values
without examining every value.
You could build it up pairwise with Math.min() or use
java.util.Collections.min() over a Collection of the values (including
Integer.valueOf(99) ).

Signature
Lew
Thomas Hawtin - 19 Aug 2007 16:55 GMT
> I'm writing a code, and to save space I'm wondering how I could do
> this. There's no easy way to do this so I'll just give an example:
Don't worry so much about space as complexity and understandability.
> int blueCoins = playerBlueCoins;
> int redCoins = playerRedCoins;
[quoted text clipped - 4 lines]
> greater than 99, the value is 99" without having to write it for every
> single int?
Well, you could write a class to represent a number of coins in this
context. Otherwise you are left with:
int blueCoins = Math.min(playerBlueCoins , 99);
...
Tom Hawtin
Patricia Shanahan - 19 Aug 2007 16:58 GMT
> I'm writing a code, and to save space I'm wondering how I could do
> this. There's no easy way to do this so I'll just give an example:
[quoted text clipped - 7 lines]
> greater than 99, the value is 99" without having to write it for every
> single int?
Any time you find yourself saying something like "for every single int"
reconsider whether you should have an array or map that could be
processed in a for-loop, rather than several separate variables.
Patricia