为了账号安全,请及时绑定邮箱和手机立即绑定

获取不断变化的Integer的最新Int 2值

获取不断变化的Integer的最新Int 2值

MMMHUHU 2021-04-08 17:15:09
我正在尝试检查一个不断变化的速度整数中的值,我要检查的值是当前值以及它之前的值,我有这段代码,但是由于某些原因它无法正常工作。我的问题是如何有效保存不断变化的Speed整数的最新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 */ }
查看完整描述

2 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

您可以添加一个temp变量来存储的值CurrentValue。


int CurrentValue;

int PriorValue;

int temp = CurrentValue; //Add a temp variable to store CurrentValue.

CurrentValue = ChangingInt;        

PriorValue = temp;


查看完整回答
反对 回复 2021-04-21
?
PIPIONE

TA贡献1829条经验 获得超9个赞

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在运行之后,我们将不需要,因为我们只需要最后两个值,所以我们应该PriorValue首先将其更改为CurrentValue(因为CurrentValue需要成为new PriorValue),以进行重新分配。然后,我们可以更改CurrentValue为ChangingInt。


查看完整回答
反对 回复 2021-04-21
  • 2 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信