使用for循环来确定int变量的最大值 [英] using a for loop to determine maximum value of an int variable

查看:101
本文介绍了使用for循环来确定int变量的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调查不同变量类型的最大大小。

我正在使用INT作为我的探索起始变量。我知道int变量可以采用的最大数量是:$ 56,535。但是我想要写一个程序来测试这个,我假设我事先并不知道这个

数。我提出以下问题,但有两个问题。

也许有人可以帮忙?


使用System;

使用System。 Collections.Generic;

使用System.Text;


命名空间ConsoleApplication1

{

class Program

{

static void Main(string [] args)

{

int j = 0;

double i = 0;

for(i = 0; i< 9999999999999999999; i ++)

{


j =(int)i;

Console.WriteLine(j.ToString());

///错误console.write(出错了。 ..这个

是错误代码)


}

}

}

}


q.1。

我想知道我的程序中发生了什么。你可以看到
,在我的for循环中,每次增加时,我都会在新的

行上将j的值写入屏幕。这太慢了。似乎是花了很长时间才能滚动数字。我的问题是,是否有一个

的方式,我可以显示j的值,比如说10万

的增量?


q.2。

有没有办法让我可以计算循环完成的时间,以及

这次循环显示结束?


q.3

我想在变量

崩溃时向屏幕输出错误消息,因为它''太小了,无法将号码分配给

。我该怎么办?


谢谢,


加里。

解决方案

我正在尝试调查不同变量


类型的最大大小。我正在使用INT作为探索的起始变量。我知道

int变量可以采用的最大数量是:65,535。



实际上,这是一个ushort的最大尺寸。 int的最大上限为

2,147,483,647。事实上,您可以通过检查int的MaxValue

字段来查看上限。


但我正在尝试写一个程序测试这个,假设我事先并不知道这个数字。我想出了以下内容,但有两个

的问题。也许有人可以帮忙?


使用System;

使用System.Collections.Generic;

使用System.Text;

命名空间ConsoleApplication1

{

class program

{

static void Main(string [ ] args)

{

int j = 0;

double i = 0;

for(i = 0; i< 9999999999999999999; i ++)

{

j =(int)i;

Console.WriteLine(j.ToString()) ;

///错误console.write(出错...这个

是错误代码)

}

}

}

}

q.1。

我想要一个了解我的计划中发生了什么。你可以看到
,在我的for循环中,每次增加时,我都会在新的

行上将j的值写入屏幕。这太慢了。似乎是花了很长时间才能滚动数字。我的问题是,是否有我b $ b $

的方式我可以显示j的值,比如10万

的增量?



肯定。 i + = 10000


q.2。

有一种方法我可以计算循环完成的时间,以及

这次循环结束显示?



如果您使用的是.NET 2.0,请尝试使用System.Diagnostics.Stopwatch类。


q.3

我想在变量

崩溃时向屏幕输出错误信息,因为它太小而无法保存分配的数字

它。我该怎么做呢?



嗯,你真的不需要这样做。它将自行崩溃并且

向控制台显示错误消息。但是,您可以在try / catch块中包装代码

,如下所示:


static void Main(string [] args)

{

试试

{

//你的代码在这里......

}

catch(exception ex)

{

Console.WriteLine(ex.Message);

}

}


最诚挚的问候,

Dustin Campbell

Developer Express Inc.

< br>


ga********@myway.com 写道:


我正在尝试研究不同变量类型的最大大小。



如果你想弄清楚哪种数据类型最适合存储

某些数据,试试这个参考表:
http://msdn2.microsoft.com/en- us / library / exx3b86w.aspx


非常感谢Dustin你的答案回答了我90%的问题

并且真的给了我很多帮助。


虽然将for interation从1更改为更高的数字但是这个测试的工作原理是

它实际上并不是真的回答我的for循环问题,这可能是因为我没有在第一个

实例中提出我的问题!在这个

的例子中,我对这个问题很感兴趣,但仍然不知道答案。


我的第一个问题是: -


这样的for循环: -


for(i = 0; i< 9999999999; i ++)

{


}


如何在循环的第n个迭代中执行操作?所以对于

实例每次循环执行10,000次做什么?


另外一个问题已经出现在我身上了,因为我已经完成了你的工作>
答案。

我的代码现在显示整数变量的值到

屏幕,我不必等待,只要我我每次迭代都会增加500美元的价值。然而。当值达到:

2145,000,000所有未来的增量都不会产生错误。

而是每个未来的增量显示为:-2147483648。


我只是想知道为什么不会产生错误。为什么价值

变为负数,其数值大于允许该变量持有的实际

数值?


谢谢!


Gary-


Dustin Campbell写道:


我正在尝试调查不同变量

类型的最大大小。我正在使用INT作为探索的起始变量。我知道

int变量可以采用的最大数量是:65,535。



实际上,这是一个ushort的最大尺寸。 int的最大上限为

2,147,483,647。事实上,您可以通过检查int的MaxValue

字段来查看上限。


但我正在尝试写一个程序测试这个,假设我事先并不知道这个数字。我想出了以下内容,但有两个

的问题。也许有人可以帮忙?


使用System;

使用System.Collections.Generic;

使用System.Text;

命名空间ConsoleApplication1

{

class program

{

static void Main(string [ ] args)

{

int j = 0;

double i = 0;

for(i = 0; i< 9999999999999999999; i ++)

{

j =(int)i;

Console.WriteLine(j.ToString()) ;

///错误console.write(出错...这个

是错误代码)

}

}

}

}

q.1。

我想要一个了解我的计划中发生了什么。你可以看到
,在我的for循环中,每次增加时,我都会在新的

行上将j的值写入屏幕。这太慢了。似乎是花了很长时间才能滚动数字。我的问题是,是否有我b $ b $

的方式我可以显示j的值,比如10万

的增量?



肯定。 i + = 10000


q.2。

有一种方法我可以计算循环完成的时间,以及

这次循环结束显示?



如果您使用的是.NET 2.0,请尝试使用System.Diagnostics.Stopwatch类。


q.3

我想在变量

崩溃时向屏幕输出错误信息,因为它太小而无法保存分配的数字

它。我该怎么做呢?



嗯,你真的不需要这样做。它将自行崩溃并且

向控制台显示错误消息。但是,您可以在try / catch块中包装代码

,如下所示:


static void Main(string [] args)

{

试试

{

//你的代码在这里......

}

catch(exception ex)

{

Console.WriteLine(ex.Message);

}

}


最诚挚的问候,

Dustin Campbell

Developer Express Inc.


I''m trying to investigate the maximum size of different variable types.
I''m using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is: 65,535. But i''m
trying to write a program to test this, assuming I didn''t know this
number in advance. I came up with the following but have two questions.
Maybe someone can help?

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 9999999999999999999; i++)
{

j = (int)i;
Console.WriteLine(j.ToString());
///on error console.write(there was an error... this
was the error code)

}
}
}
}

q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there a
way that I can display the value of j every, say, 10 thousand
increments?

q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?

q.3
I''d like to output an error message to the screen when the variable
crashes because it''s too small to hold the number being allocated to
it. How do i do this?

Thankyou,

Gary.

解决方案

I''m trying to investigate the maximum size of different variable

types. I''m using INT as my starting variable for exploration. I know
that the maximum number that the int variable can take is: 65,535.

Actually, that''s the maximum size of a ushort. int has a maximum upper limit
of 2,147,483,647. In fact, you can see the upper limit by checking the MaxValue
field of int.

But i''m trying to write a program to test this, assuming I didn''t know
this number in advance. I came up with the following but have two
questions. Maybe someone can help?

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 9999999999999999999; i++)
{
j = (int)i;
Console.WriteLine(j.ToString());
///on error console.write(there was an error... this
was the error code)
}
}
}
}
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there
a
way that I can display the value of j every, say, 10 thousand
increments?

sure. i += 10000

q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?

If you''re in .NET 2.0, try the System.Diagnostics.Stopwatch class.

q.3
I''d like to output an error message to the screen when the variable
crashes because it''s too small to hold the number being allocated to
it. How do i do this?

Well, you shouldn''t actually need to do this. It''ll crash on its own and
display an error message to the console. However, you could wrap your code
in a try/catch block like this:

static void Main(string[] args)
{
try
{
// Your code here...
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.



ga********@myway.com wrote:

I''m trying to investigate the maximum size of different variable types.

If you''re trying to figure out which data types are best for storing
certain data, try this reference table:
http://msdn2.microsoft.com/en-us/library/exx3b86w.aspx


Thankyou very much Dustin your answers have answerd 90% of my questions
and have really helped me alot.

Although changing the for interation from 1, to a higher number works
for this test it doesn''t really answer my for loop question, which was
probabally because I didn''t put my question right in the first
instance! I was intrigued about the question when it came to me in this
example and still dont really know the answer.

My First Question is: -

With a for loop like so: -

for(i=0; i<9999999999; i++)
{

}

how can I do an action every nth interations of the loop? so for
instance everytime the loop has executed 10,000 times do something?

Also another question has come to me now as I have worked through your
answers.
My code is now displaying the value of of the integer variable to
screen and I''m not having to wait as long as i am incrementing it''s
value by 500,000 every iteration. However. When the value gets to:
2145,000,000 all future increments don''t generate an error.
Instead each future increment is displayed as: -2147483648.

I''m just wondering why an error isn''t generated. And why the value
becomes negative, with a numerical value one greater than the actual
numerical value that the variable is permitted to hold?

Thanks!

Gary-

Dustin Campbell wrote:

I''m trying to investigate the maximum size of different variable
types. I''m using INT as my starting variable for exploration. I know
that the maximum number that the int variable can take is: 65,535.


Actually, that''s the maximum size of a ushort. int has a maximum upper limit
of 2,147,483,647. In fact, you can see the upper limit by checking the MaxValue
field of int.

But i''m trying to write a program to test this, assuming I didn''t know
this number in advance. I came up with the following but have two
questions. Maybe someone can help?

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int j = 0;
double i = 0;
for (i = 0; i < 9999999999999999999; i++)
{
j = (int)i;
Console.WriteLine(j.ToString());
///on error console.write(there was an error... this
was the error code)
}
}
}
}
q.1.
I want to have an idea of what is going on in my program. As you can
see, in my for loop I am writing the value of j to the screen on a new
line every time it is incremented. This is too slow. It seems to be
taking forever to scroll through the digits. My question is, is there
a
way that I can display the value of j every, say, 10 thousand
increments?


sure. i += 10000

q.2.
is there a way I can time how long the loop takes to complete, and at
the end of the loop display this time?


If you''re in .NET 2.0, try the System.Diagnostics.Stopwatch class.

q.3
I''d like to output an error message to the screen when the variable
crashes because it''s too small to hold the number being allocated to
it. How do i do this?


Well, you shouldn''t actually need to do this. It''ll crash on its own and
display an error message to the console. However, you could wrap your code
in a try/catch block like this:

static void Main(string[] args)
{
try
{
// Your code here...
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.


这篇关于使用for循环来确定int变量的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆