AI智能
改变未来

C#学习.获取数组最大值,最小值,总和,平均值

        static void Main(string[] args)
        {
            int[] nums = { 2, 5, 3, 6, 4, 7, 1, 8, 9 };
            int max, min, sum, avg;
            Test(nums, out max, out min, out sum, out avg);
            Console.WriteLine(\”最大值{0},最小值{1},总和{2},平均值{3}\”, max, min, sum, avg);
            Console.Read();
        }
        /// <summary>
        /// 计算一个数组的最大值,最小值,总和,平均值
        /// </summary>
        /// <param name=\”nums\”>需要传入的数组</param>
        /// <param name=\”max\”>返回的最大值</param>
        /// <param name=\”min\”>返回的最小值</param>
        /// <param name=\”sum\”>返回的总和</param>
        /// <param name=\”avg\”>返回的平均值</param>
        public static void Test(int[] nums,out int max,out int min,out int sum,out int avg)
        {
            max = nums[0];
            min = nums[0];
            sum = 0;
            avg = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if(max < nums[i])
                {
                    max = nums[i];
                }
                if(min > nums[i])
                {
                    min = nums[i];
                }
                sum += nums[i];
            }
            avg = sum / nums.Length;
        }

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C#学习.获取数组最大值,最小值,总和,平均值