所以我有这个东西可以显示输入的一系列数字中的最小和最大数字,请注意我只是一个初学者,还没有学习数组或排序:
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
double number, maxValue= Double.MinValue , minValue= Double.MaxValue;
string goOn = "Y";
Console.WriteLine("Please enter a series of numbers, when you wish to stop entering numbers please enter -99.");
Console.WriteLine("The smallest and largest values will then be displayed.");
Console.WriteLine("Remember not to enter -99 unless you want the series to end.");
do
{
while (!double.TryParse(Console.ReadLine(), out number))
Console.WriteLine("Please enter whole numbers only");
while (number != -99)
{
process(ref minValue, ref maxValue, number);
while (!double.TryParse(Console.ReadLine(), out number))
Console.WriteLine("Please enter whole numbers only");
}
Console.WriteLine("The smallest value is {0} and the largest value is {1}.", minValue, maxValue);
Console.WriteLine("Do you want to enter another series of numbers?");
Console.WriteLine("If so enter y, if you want to end press any other key");
goOn = Console.ReadLine();
if (goOn.ToUpper() == "Y")
{
Console.WriteLine("Please enter your set of numbers.");
Console.WriteLine("Remember not to enter -99 unless you want the series to end.");
}
} while (goOn.ToUpper() == "Y");
}
static void process(ref double minValue, ref double maxValue, double number)
{
if (number > maxValue)
{
maxValue = number;
}
if (number < minValue)
{
minValue = number;
}
}
}
}
但是当我输入一系列数字,然后停止,然后制作另一个数字列表时,它会保留上一个列表中的最小值和最大值,如下所示:
我也在做我自己版本的其他人的程序,但无法弄清楚他如何通过设置 maxValue = 0 和 minValue = 0 来做到这一点。他确实使用 Int64 而不是 double 并使用三个模块代替1 和另一个 if 语句,如果 minValue 和 maxValue 为 0。