我正在尝试检查不断变化的速度整数中的值,我要检查的值是当前值和它之前的值,我有此代码,但由于某种原因它不起作用。 我的问题是如何有效地保存不断变化的速度整数的最新 2 个值
这是我的代码
int CurrentValue; // The Integer is already defined and initialized, this is the latest value of it
int PriorValue; // this value is the one that was prior to the current one
CurrentValue = ChangingInt; // here we save the current Speed value in ChangingInt
if (CurrentValue != ChangingInt){
PriorValue = CurrentValue; /* replacing the CurrentValue value as the prior one
since it has changed according to the if statement */
}
您可以添加一个temp
变量来存储CurrentValue
的值。
int CurrentValue;
int PriorValue;
int temp = CurrentValue; //Add a temp variable to store CurrentValue.
CurrentValue = ChangingInt;
PriorValue = temp;
int ChangingInt = ... // ChangingInt = A, PriorValue = B, CurrentValue = C
...
int PriorValue = CurrentValue // ChangingInt = A, PriorValue = C, CurrentValue = C
int CurrentValue = ChangingInt // ChangingInt = A, PriorValue = B, CurrentValue = A
在此运行之后,我们将不需要PriorValue
,因为我们只需要最后两个值,因此我们应该首先通过将其更改为CurrentValue
来重新分配PriorValue
(因为CurrentValue
需要成为新的PriorValue
(。然后,我们可以将CurrentValue
更改为ChangingInt
.