c#编写计算并输出一维(9.8,12,45,67,23,1.98,2.55,45)求最大最小平均值

2024-11-26 04:34:27
推荐回答(1个)
回答1:

 public static void Main(string[] args)
        {
            double[] a = { 9.8, 12, 45, 67, 23, 1.98, 2.55, 45 };
            double min = 10000;
            double max = 0;
            for (int i = 0; i < a.Length - 1; i++) //找出数组的最大最小值
            {
                if (a[i] > a[i + 1])
                {
                    min = a[i + 1];
                    if (a[i] > max)
                    {
                        max = a[i];
                    }
                    else
                    {
                        i++;
                    }
                }
                else
                {
                    min = a[i];
                    max = a[i + 1];
                }
            }
            Console.WriteLine((min+max)/2);
            Console.ReadKey();
        }