产生负数的正加法运算

Positive addition operations producing a negative number

我正在编写一个脚本来从 csv 文件中获取一系列数字并计算它们的总和。

我已将 csv 中的值提取到 List<string> 中,然后循环遍历以将它们加在一起。 这些数字是一天中每一分钟的毫秒表示,因此通常从 0 开始并递增 6000

但出于某种原因,最终数字似乎是负数。 我在加法运算结束时检查,最终计数小于 1.

我尝试将数字打印到控制台并且它们是正确的,我猜其他地方有问题吗?

Screenshot of sample out

提前致谢。

var totalSeconds = 0;
var minutesCounted = 0;
var unzippedFolder = Compression.UnzipToFolder(zipPath);
var listOfSeconds = ReadCsvIndex(unzippedFolder[0], ",", 0, true);

foreach (var second in listOfSeconds)
{
   // Console.WriteLine(Int32.Parse(second)); // Prints correct numbers
   totalSeconds += Int32.Parse(second);
   minutesCounted++;
   Console.WriteLine(minutesCounted + totalSeconds);
}

Console.WriteLine(security + totalSeconds);
Console.WriteLine(minutesCounted);
File.Delete(unzippedFolder[0]);

if (totalSeconds > 1)
{
  Console.WriteLine(true);
}
else
{
   Console.WriteLine(false); // This is returning false
}
Console.ReadLine();

使用 long 值代替 totalSeconds 的默认值 int。 该值显然高于 Int32.MaxValue,因此该值环绕为负值。

A long 有一个更高的最大值,所以你不会得到这个溢出效果(至少,不会很快)