输入任意多的学生的成绩,计算总分数与平均分数

来源:互联网 发布:大数据交通分析平台 编辑:程序博客网 时间:2024/04/29 10:42

 

 

 

static void Avg(ArrayList myArry)
        {
            Console.WriteLine("注意:输入成绩时当你输入0是只代表结束输入!,数组中没有0这一项。");
            Console.WriteLine("请输入成绩:");
            float scores, sumScores=0;
            int i;
            for (i = 0;; i++)
            {
                try
                {
                    scores = Convert.ToSingle(Console.ReadLine());
                    if (scores ==0)
                    {
                        break;
                    }
                    myArry.Add(scores);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            for (int j = 0; j < myArry .Count; j++)
            {
                sumScores +=Convert.ToSingle( myArry[j]);
            }
            Console.WriteLine("你输入了{0}个分数,有{1}有效地分数,总分为:{2}分,平均分为:{3}分", i, myArry.Count, sumScores, sumScores / myArry.Count);
        }
       

原创粉丝点击