(c#第二天)输入10个整数统计偶数个数(方法调用,try catch)

    public class MainClass
    {
        public static int countEventNum(int[] arr)
        {
            try      //可能出现异常的语句快
           
            {
                int count = 0;
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i] % 2 == 0)
                    {
                        count++;
                    }
                }
                return count;
            }
            catch (Exception ex)    // 若有异常,在此抛出
            {
                throw ex;
            }
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                string s;
                int i = 0;
                int[] a = new int[10];
                while (i < a.Length)
                {
                    Console.Write("请输入第{0}个整形数字:", i + 1);
                    s = Console.ReadLine();
                    int.TryParse(s, out a[i]);  //使用TryParse方法代替无效转型异常
                    i++;
                }
                int k = MainClass.countEventNum(a); //调用统计偶数个数方法
                Console.WriteLine("偶数个数是" + k.ToString());
                Console.ReadKey(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

}

//不输入视为偶数(可能视为0)

(c#第二天)输入10个整数统计偶数个数(方法调用,try catch)