Possible to group different ints to do one command?

R

rckildea

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?
 
L

Lew

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?

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.
 
L

Lew

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?

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) ).
 
P

Patricia Shanahan

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?

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
 
T

Thomas Hawtin

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;
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top