写一个找到最小给定数字的程序?我该怎么办? [英] write a program that find smallest number of given number? what should i do?

查看:82
本文介绍了写一个找到最小给定数字的程序?我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int  number; 
int min_value = 100000 ;
for int i = 0 ; i < = 4 ; i ++)
{
Console。 WriteLine( 输入数字:);
number = Convert.ToInt16(Console.ReadLine());
if (number < min_value)
min_value = number;

Console.WriteLine(number);
}

解决方案

请参阅问题的评论 - 由Matt T Heffron和我的。这就是问题。



这是解决方案:而不是毫无意义的100000使用 int.MaxValue (并且,要找到最大值,请以 int.MinValue 开头):

http://msdn.microsoft.com/en-us/library/system.int32.minvalue.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.int32。 maxvalue.aspx [ ^ ]。



所有其他整数类型都有类似的属性。



浮点数类型,事情更有趣。你应该使用 NegativeInfinity PositiveInfinity

http://msdn.microsoft.com/en-us/library/system.double.negativeinfinity.aspx [< a href =http://msdn.microsoft.com/en-us/library/system.double.negativeinfinity.aspxtarget =_ blanktitle =New Window> ^ ],
http://msdn.microsoft.com/en-us/library /system.double.positiveinfinity.aspx [ ^ ]。



基本上重要且令人惊奇的特征是:无穷大值与运营商''>的非无限值正确比较'',''<'',''> ='',''< =''和''==''!



< DD> -SA

int number;
int min_value = 100000;
for (int i = 0; i <= 4; i++)
{
   Console.WriteLine("Enter a number: ");
   number = Convert.ToInt16(Console.ReadLine());
   if (number < min_value)
      min_value = number;

   Console.WriteLine(number);
}

解决方案

Please see comments to the questions — by Matt T Heffron and mine. This is the problem.

And here is the solution: instead of pointless "100000" use int.MaxValue (and, to find the maximum, start with int.MinValue):
http://msdn.microsoft.com/en-us/library/system.int32.minvalue.aspx[^],
http://msdn.microsoft.com/en-us/library/system.int32.maxvalue.aspx[^].

All other integer types have similar property.

With floating-point types, things are way more interesting. You should use NegativeInfinity and PositiveInfinity:
http://msdn.microsoft.com/en-us/library/system.double.negativeinfinity.aspx[^],
http://msdn.microsoft.com/en-us/library/system.double.positiveinfinity.aspx[^].

The fundamentally important and amazing feature is: infinity values correctly compare with non-infinite values with operators ''>'', ''<'', ''>='', ''<='' and ''==''!

—SA


这篇关于写一个找到最小给定数字的程序?我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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