迭代直到int.MaxValue达到 [英] Iterating until int.MaxValue reached
问题描述
作为一个小测试,我想看看它会需要多长时间才能数到int.MaxValue在C#控制台应用程序。每隔几个小时我检查的进展情况。昨天晚上,当我认为该计划将进行执行时,它正在执行回0。我不知道为什么会发生,我想知道,如果任何人都可以解释给我。它的所作所为是,就以此为2,147,483,647那么当该值达到它开始倒数到零。即使它似乎是倒数,该值有一个负数。我不知道如果我需要使用int.MaxValue的绝对值。反正我只是好奇,如果任何人都可以看到,我没有看到。这里是我的代码。谢谢
静态无效的主要(字串[] args)
{
INT MAXINT = int.MaxValue;
INT numToCountTo = MAXINT;
// INT numToCountTo = 1000000;
试
{
Console.WriteLine(DateTime.Now);
Console.WriteLine(按任意键开始。);
到Console.ReadLine();
秒表SW =新的秒表();
sw.Start();
为(INT计数器= 0;反< = numToCountTo;反++)
{
Console.WriteLine(柜);
}
sw.Stop();
时间跨度TS = sw.Elapsed;
//格式和显示的时间跨度值。
串elapsedTime =的String.Format({0:00时},{1:00分},{2:00秒},{3:00毫秒},
ts.Hours,TS .Minutes,ts.Seconds,
ts.Milliseconds / 10);
//(#,#)则以每3位
Console.WriteLine之间用逗号(花+ elapsedTime +数到+ numToCountTo.ToString( #,#));
Console.WriteLine(按回车键继续。);
到Console.ReadLine();
}
赶上(异常前)
{
抛出新的异常(异常达到:+ ex.Message)
}
}
您的循环:
的for(int柜台= 0;反< = numToCountTo;反++)
不正确。它将执行,而计数器< = int.MaxValue
这是总是如此。当它增加它,它会滚到 int.MinValue
,并保持递增。
将其更改为
的for(int柜台= 0;反< numToCountTo;反++)
或使用长
为您的循环计数器:
为(长计数器= 0;反< = numToCountTo;反++)
您也可以使用做...而
循环,因为执行循环的前的断裂评估条件:
INT计数器= 0;
做
{
:
计数器++;
}
,而(计数器< numToCountTo);
As a little test I wanted to see how long it would take to count to int.MaxValue in a C# console application. Every few hours I checked the progress. Last night when I thought the program would be done executing, it was executing back to 0. I'm not sure why that happened and I was wondering if anyone could explain it to me. What it did was that it counted to 2,147,483,647 then when this value was reached it began counting backwards to zero. Even though it appeared to be counting backwards, the value had a negative number. I wonder if I needed to use the absolute value of int.MaxValue. Anyway, I was just curious if anyone could see what I'm not seeing. Here is my code. Thanks
static void Main(string[] args)
{
int maxInt = int.MaxValue;
int numToCountTo = maxInt;
//int numToCountTo = 1000000;
try
{
Console.WriteLine(DateTime.Now);
Console.WriteLine("Press any key to begin.");
Console.ReadLine();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int counter=0; counter <=numToCountTo ; counter++)
{
Console.WriteLine(counter);
}
sw.Stop();
TimeSpan ts = sw.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00 Hours}, {1:00 Minutes}, {2:00 Seconds}, {3:00 Milliseconds}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
// ("#,#") places a comma between every 3 digits
Console.WriteLine("It took " + elapsedTime + " to count to " + numToCountTo.ToString("#,#"));
Console.WriteLine("Press Enter key to continue.");
Console.ReadLine();
}
catch (Exception ex)
{
throw new Exception("Exception Reached: " + ex.Message)
}
}
Your for loop:
for (int counter=0; counter <=numToCountTo ; counter++)
is incorrect. It will execute while counter <= int.MaxValue
which is ALWAYS true. When it increments it it will roll to int.MinValue
and keep incrementing.
Change it to
for (int counter=0; counter < numToCountTo ; counter++)
or use a long
for your loop counter:
for (long counter=0; counter <= numToCountTo ; counter++)
You can also use a do...while
loop since the loop is executed before the breaking condition is evaluated:
int counter = 0;
do
{
...
counter++;
}
while(counter < numToCountTo);
这篇关于迭代直到int.MaxValue达到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!