c# - C# 中的数字列表并获取最小值和最大值,而不保留上一个循环中存储的变量

标签 c# loops if-statement numbers

所以我有这个东西可以显示输入的一系列数字中的最小和最大数字,请注意我只是一个初学者,还没有学习数组或排序:

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;
            }

        }
    }
}

但是当我输入一系列数字,然后停下来,然后制作另一个数字列表时,它会保留前一个列表中的最小值和最大值,如下所示:

Console example showing numbers 6, 7, -7, 3, 5, 100, 44.  -7 is the minimum value and 100 is the maximum.  Then another list is made, 5, 55, 34, but -7 is still the minimum and 100 is still the maximum

我也正在做我自己版本的别人的程序,就像这样,但无法弄清楚他如何通过设置 maxValue = 0 和 minValue = 0 来做到这一点。他确实使用 Int64 而不是 double 并使用三个模块(而不是 1)和另一个 if 语句(如果 minValue 和 maxValue 为 0)。

最佳答案

如果您需要在新序列中搜索最小值和最大值,只需清除以前保存的值即可:

if (goOn.ToUpper() == "Y")
{
    maxValue= Double.MinValue;
    minValue= Double.MaxValue;
    Console.WriteLine("Please enter your set of numbers.");
    Console.WriteLine("Remember not to enter -99 unless you want the series to end.");
}

关于c# - C# 中的数字列表并获取最小值和最大值,而不保留上一个循环中存储的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15208936/

相关文章:

css - 如何在嵌套 sass 映射 (SCSS) 中执行 'if' 语句?

c# - 如何在容器之间复制 blob?

c - 循环平铺优化

jQuery 如果点击

C#、For 循环和速度测试……完全相同的循环第二次更快?

python - 获取数组python中的最小数字

javascript - 有没有办法在 VUE html 中编写 "If Greater than"和 "If less than"语句?

c# - 属性面板中缺少 Datagrid `TopLeftHeaderCell`

c# - 如何以编程方式将俄语音译为英语?

c# - WCF 服务配置 : How can I access the AppSettings in code using C#