c#中的变量重置

本文关键字:变量 | 更新日期: 2023-09-27 18:03:17

我有一个应用程序,我需要一个不断递增的变量。

的例子:

float dataStream = test1 - test2;
test0 = dataStream;

但是当数据流重置时,test0也会重置。我怎么能防止这种情况,所以test0保持它以前的数字,所以当数据流获得数据时,它添加到test0 ?我无法理解它

编辑:添加了我的代码截图http://prntscr.com/7u42i1

代码在一个不断运行的函数中运行,所以+=不起作用,因为它在不断递增(超过dataStream)

Edit2

dataStream从API流数据,test0是存储数据的变量。

当数据流得到一个新值时,我想把它添加到test0上,但是我尝试过的每种方法总是超过。

//firstFuelCount - data.Drivetrain.Fuel is the calculation that needs to be added onto test3
//When refueling, firstFuelCount - data.Drivetrain.Fuel decreases, so does test3
test2 = data.Drivetrain.FuelMax - data.Drivetrain.Fuel;
if (test2 < test3)
{
}
else
{
    test3 = data.Drivetrain.FuelMax - data.Drivetrain.Fuel;                    
}
基本上

float dataStream = test1 - test2;
test0 = dataStream;
dataStram = 5
test0 = 5
dataStream = 1
test0 = 6

等。

c#中的变量重置

总算弄明白了。在这个过程中可能老了100年

            //Only for testing
            var fakeCurrFuelDrive = fakeCurrFuel;
            //Check if the engine is running before calcualting burned lites
            if (data.Drivetrain.EngineRpm >= 50)
            {
                //Ensure variable has data
                if (drivenOldFuel == 0)
                {
                    //Set variable for next check
                    drivenOldFuel = fakeCurrFuelDrive;
                }
                //Distance travelled - refueling
                if ((drivenOldFuel - fakeCurrFuelDrive) < 0)
                {
                    drivenCurrentFuel = fuel + (fakeCurrFuelDrive - drivenOldFuel);
                }
                //Check for refueling
                if ((drivenOldFuel - fakeCurrFuelDrive) > 0)
                {
                    drivenCurrentFuel = fuel + (drivenOldFuel - fakeCurrFuelDrive);
                }
                //Set variable for next check
                drivenOldFuel = fakeCurrFuelDrive;
                fuel = drivenCurrentFuel;
            }