问答详情
源自:6-8 最终项目

希望能更优化下

string[,] scores = new string[8,2]{{"景珍","90"},{"林惠洋","65"},{"成蓉","88"},{"洪南昌","70"},{"龙玉民","46"},{"单江开","81"},{"田武山","100"},{"王三明","68"}};

          int avg=0;//平均分

            for(int i=0;i<scores.GetLongLength(0);i++){

                avg+=Convert.ToInt32(scores[i,1]);

            }

            avg = avg/8;

    

    string name ="";//高于平均分的名字

    for(int i=0;i<scores.GetLongLength(0);i++){

        if(Convert.ToInt32(scores[i,1])>=avg){

            name+=scores[i,0]+" ";

        }

   }

    name = name.Trim(' ');

    Console.WriteLine("平均分是{0},高于平均分的有:",avg);

    Console.WriteLine(name);


提问者:星银色飞行船 2020-02-04 19:33

个回答

  • ddodd
    2020-02-10 00:09:39

                string[] name = new string[] {"景珍", "林慧洋", "成蓉", "洪南昌", "龙玉民", "单江开", "田武山", "王三明"};
                int[] score = new int[] {90,65,88,70,46,81,100,68};
                int sum=0,avg;
                for(int i=0;i<score.Length;i++)
                {
                    sum += score[i];
                }
                avg = sum / score.Length;
                int index = 0;
                
                Console.WriteLine("平均分是{0},高于平均分的有:", avg);
                for (int x = 0; x < score.Length; x++)
                {
                    if (score[x] > avg)
                    {
                        index = x;
                        Console.Write(name[index]+" ");
                    }
                }