我想问一下大家,我这个“最高分者”跟“高于平均分的人”都不对,我找不到原因,有人帮我看一下吗?谢谢你们

来源:6-8 最终项目

慕仙8563930

2017-09-25 22:27

   static void Main(string[] args)        {           string[] name = new string[2];           int[] score = new int[2];           for (int i=0;i<name.Length;i++)            {                Console.WriteLine("输入第" + (i + 1) + "位的姓名:");                name[i] = Console.ReadLine();                Console.WriteLine("输入第" + (i + 1) + "位的分数:");                score[i] = int.Parse(Console.ReadLine());            }            int sum=0,avg;            for (int i=0;i<score.Length;i++)            {                sum += score[i];            }            avg = sum / score.Length;            Console.WriteLine("总分数:{0}平均分:{1}",sum,avg);            int max=0;            string topone=null;            for (int j=1;j<score.Length;j++)            {                if (score[j] > max)                    max = score[j];                    topone = name[j];            }            Console.WriteLine("最高分是{0}", max);            Console.WriteLine("最高分者是{0}",topone);            int avgp=score[0];            string avgpp =name[0];            for (int k=1;k<score.Length;k++)            {                if (score[k] > avg)                    avgp = score[k];                    avgpp = name[k];            }            Console.WriteLine("高于平均分的人有{0}",avgpp);            Console.ReadLine();        }    }}

写回答 关注

2回答

  • valenbine
    2017-09-26 12:14:59

    看错了 以为你写的是最后一个题呢 我直接把我编的发出来了  我看了一下你那个  可以实现你要的功能啊

    valenb...

    你这个已经是实现了啊 就是只能输入两组数据

    2017-09-26 18:16:21

    共 2 条回复 >

  • valenbine
    2017-09-26 12:02:13

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace projAboveAvg
    {
        class Program
        {
                    static int Avg(int[] scores)
            {
                int avgscore=0;
                int sum = 0;
                foreach (var score in scores)
                {
                    sum += score;
                }
                avgscore = sum / scores.Length;
                return avgscore;
            }
            static void BeyondAvg(int[] scores,string[] names)
            {
                int i;
                int avg = Avg(scores);
                for (i = 0; i < scores.Length; i++)
                {
                    if (scores[i]>avg)
                    {
                        Console.Write(names[i] + " ");
                    }
                }
            }
            static void Main(string[] args)
            {
                int[] scores = { 90, 65, 88, 70, 46, 81, 100, 68 };
                string[] names = { "景珍", "林惠洋", "成蓉", "洪南昌", "龙玉民", "单江开", "田武山", "王三明" };
                Console.Write($"平均分是{Avg(scores)},");
                Console.WriteLine("高于平均分的有:");
                BeyondAvg(scores, names);
            }
        }
    }


C#开发轻松入门

本门课程是C#语言的入门教程,将带你轻松入门.NET开发

254118 学习 · 1459 问题

查看课程

相似问题