1使用C#编写一个控制台应用。输入若干个正整数存入数组中(输入exit表示输入结束),输出最大值、最小值

//课堂上没有演示成功,问题出在:arraylist对象不能做参数

//源代码参见之下:

//使用C#编写一个控制台应用。输入若干个正整数存入数组中(输入exit表示输入结束),输出最大值、最小值和平均值 

 

 using System;
 using System.Collections;
 class Program
{
    static void Main(string[] args)
    {
        try
        {

            #region 1.数据输入部分
            ArrayList al = new ArrayList();
            while (true)
            {


                string s = Console.ReadLine();
                if (s.ToLower().Equals("exit")) break;//————————————————————————————————————————————————————
                int x;
                bool flag = int.TryParse(s, out x);//————————————————————————————————————————————————————
                if (!flag || x < 0 || (x == 0 && (s.Trim().Length <= 0 || !s.Equals("0"))))
                {
                    Console.WriteLine("输入数字无效!");
                    continue;
                }

                al.Add(x);
            }
            int[] arr = new int[al.Count];
            for (int i = 0; i < al.Count; i++)
            {
                arr[i] = int.Parse(al[i].ToString());//————————————————————————————————————————————————————
            }
            #endregion

            #region 2.调用方法进行业务处理
            int a, b, c;
            MyClass.MyPross(out a, out b, out c, arr);
            #endregion

            #region 3.数据输出部分
            Console.WriteLine("max={0},min={1},avg={2}", a, b, c);
            #endregion

            Console.ReadKey(false);
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);//————————————————————————————————————————————————————
        }
    }
}
class MyClass
{
    #region 具体业务处理方法
    public static void MyPross(out int max, out int min, out int avg, params int[] arr)
    {
        try
        {

            #region 业务处理
            max = 0;
            min = 0;
            avg = 0;
            if (arr.Length <= 0) return;
            foreach (int item in arr)
            {
                if (item > max) max = item;
                if (item < max) min = item;
                avg += item;
            }
            avg = avg / arr.Length;

            #endregion
        }
        catch (Exception ex)
        {
            throw ex;//————————————————————————————————————————————————————
        }
    }
    #endregion
}

 

你可能感兴趣的:(C#)