java - Variable does not increment in probability program -
i have 3 classes. super class probability, subclass eating , runner class person.
the goal methods "triggered" when conditions met in loop. eating (method dox) increase variable weight. once weight greater 120, probability (int px) of dox being called decreases , probability (py) of exercising increases until weight drops below threshold.
however int px never decreases. why this? below decrease probability methods , trigger calls them.
public void trigger() { if (weight >= 120) { dprob(px); dprob(pz); iprob(py); } if (weight <= 80) { dprob(py); iprob(px); iprob(pz); } public void dprob(int p) { p -= 5; } public void iprob (int p) { p += 5; }
p local variable method dprob, , it's different variable p in iprob. meaning px, pz , py not affected (java passes by value, always).
when enter method, temporary variable created, , destroyed exit it.
you should make p class member (read more here), or let methods return result, , assign caller, or directly modify px, py , pz.
Comments
Post a Comment