Reputation: 161
hi I'm new to java and having trouble solving this.
void setNumber(int inputNumber)
{
int currentNumber = inputNumber;
int previousNumber = ??????????
}
so if inputNumber is " 2 " , then currentNumber should be " 2 " and I want to change inputNumber to something else but I want previousNumber to be " 2 " and on and on, previousNumber to have one step before currentNumber's new value. how can I do this?
Thanks!
Upvotes: 0
Views: 6628
Reputation: 3492
Just store currentNumber in previousNumber before you overwrite it with the new value.
int previousNumber = currentNumber;
int currentNumber = inputNumber;
First the previousNumber is overwritten by the currentNumber
input = 3
previous = 2
current = 2
Then the current gets the new number from input number
input = 3
previous = 2
current = 3
You now have a variable with current's previous value
Upvotes: 7
Reputation: 11648
Use Array of currentNumber
int[] currentNumber
so the current index will store the current number whereas the before indexes will point to the previously stored numbers
Upvotes: 0