gpt4 book ai didi

c# - 将正整数加到int会使它变成负数

转载 作者:太空宇宙 更新时间:2023-11-03 18:02:22 31 4
gpt4 key购买 nike

我的问题是,当我仅将正数添加到整数时,有时会变成负数。我知道加到整数上的数字永远不会为正,因为我尝试将它们放入

maths.abs()


我的代码在下面,非常感谢您的帮助

int Total = 0;
int First = 0;
int Second = 10000;
while (First <= Second)
{
Total += 5000 + (250 * First);
Console.WriteLine(Total);
First++;
}
Console.ReadKey();

最佳答案

正如其他人所提到的,您的int不能使用该数字并溢出。使用Int64代替您想要的。

Int64 Total = 0;
int First = 0;
int Second = 10000;
while (First <= Second)
{
Total += 5000 + (250 * First);
Console.WriteLine(Total);
First++;
}
Console.ReadKey();

关于c# - 将正整数加到int会使它变成负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51293459/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com