The best way to do this is to use a function. It should not look exactly like this, but:
class Whatever { private double basePower = 5000; public int numPowerUps = 5; public double GetActualPower() { return basePower + (numPowerUps * basePower * 0.1); } }
Just change numPowerUps to 0 when done. Thus, he looks much neat.
To the side:
The reason it does not work is because adding and then subtracting percent does not work. For example:
1. What is 10% of 100? --> 10 2. Add that to the 100 --> 110 3. What is 10% of 110? --> 11 4. Subtract that from 110 --> 99
You will always be 99% of your original value. If you really want to use a shortcut, you can do this:
1. What is 10% of 100? --> 10 2. Add that to the 100 --> 110 3. What is (100/11) = 9.09090909...% of 110? --> 10 4. Subtract that from 110 --> 100
But then you are potentially prone to floating point errors. A functional way to do this is not only more accurate and understandable, but also potentially less error prone.
Smashery
source share