Dont do math operations on textNumber. Its prone to errors. Store your data in appropriately named variables; its just good coding practice.
- Code: Select all
ammo = min(ammo, 154);
You dont need an if here. The opposite would be max(ammo, 154), which would prevent it from going under 154. you can use this to assure that ammo never falls below zero.
- Code: Select all
ammo = min(max(ammo, 0), 154);
now it is not possible for ammo to be anything but a range from 0 to 154
When you are coding, ask yourself "am i SURE i always want the computer to do this?" if the answer is yes, you probably dont need an if. Basically, if the decision is already known before the program runs, then you dont want to use if. You want to tell the compiler "do this, and dont think about it".
After that, assign it to the textNumber
- Code: Select all
textNumber = ammo;
and you will never see a value outside the range you want.