如何在c#中显示数组的最大值,最小值和平均值

本文关键字:最大值 最小值 平均值 数组 显示 | 更新日期: 2023-09-27 18:03:06

基本上,我必须编写一个程序,要求用户说出他们足球队中球员的数量,列出他们的名字和他们进了多少球。然后输出应该是"顶级玩家是……得分是……"平均进球数是……最低进球数是……"

我们还没有真正正确地讨论数组,但是我现在已经开始这个问题了,它会让我发疯,直到我完成它。我知道我可能离得到我需要的东西有点远,但在正确的方向上的任何一点都会非常感激!附:我知道我的代码的最后一点是完全错误的,我只是不知道从这里去哪里。我的代码:

    Console.WriteLine("Enter the amount of players");
    int amount = int.Parse(Console.ReadLine());
    string[] names = new string[amount];
    int[] playerGoals = new int[amount];
    int result;
    string playerName;
    for (int i = 0; i < playerGoals.Length; i++)
    {
        Console.WriteLine("Enter a players name");
        playerName = Console.ReadLine();
        names[i] = playerName;
        Console.WriteLine("Enter how many goals they have score this season");
        result = int.Parse(Console.ReadLine());
        playerGoals[i] = result;
    }
    int minimum = playerGoals.Min();
    int maximum = playerGoals.Max();
    double average = playerGoals.Average();
    Console.WriteLine("The top player is {0} with a score of {1}", maximum);
    Console.WriteLine("");
    Console.WriteLine("The average goals scored is {0}", average);
    Console.WriteLine("");
    Console.WriteLine("The lowest goal scored is {1}");
    Console.ReadLine();

如何在c#中显示数组的最大值,最小值和平均值

您可以采用以下方法:

  1. 查找得分最高的玩家

    string maxPlayer = names[Array.IndexOf(playerGoals, maximum)];
    
  2. 在循环中计算自己的最大值(无论是在你接受输入时还是之后),以一种你跟踪玩家的方式。

  3. 创建一个PlayerStats类,这样你就有一个数组(PlayerStats[])而不是两个,并使用MoreLINQ的MaxBy。在我看来,这最终会得到最好的代码,但可能比你准备好的更高级(知道如何手动做事情是一项很好的技能,尽管你在现实世界中并不总是使用它)。

    var best = playerStats.MaxBy(x => x.Goals);
    Console.WriteLine("The top player is {0} with a score of {1}",
                      best.Name, best.Goals);
    public class PlayerStats
    {
        public string Name { get; set; }
        public int Goals { get; set; }
    }
    
 class Player
        {
            public string Name { get; set; }
            public int goals { get; set; }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the amount of players");
            int amount = int.Parse(Console.ReadLine());
            List<Player> _players = new List<Player>();
            for (int i = 0; i < amount; i++)
            {
                Player objPlayer = new Player();
                Console.WriteLine("Enter a players name");
                objPlayer.Name = Console.ReadLine();
                Console.WriteLine("Enter how many goals they have score this season");
                objPlayer.goals  = int.Parse(Console.ReadLine());
                _players.Add(objPlayer);
            }
            int maxgoals = _players.Max(t => t.goals);
            var maxplayer = _players.FirstOrDefault(t => t.goals == maxgoals).Name;
            int mingoals = _players.Min(t => t.goals);
            var minplayer = _players.FirstOrDefault(t => t.goals == maxgoals).Name;
            var average = _players.Sum(t=>t.goals)/amount;
            Console.WriteLine("The top player is {0} with a score of {1}", maxplayer, maxgoals);
            Console.WriteLine("");
            Console.WriteLine("The bottom player is {0} with a score of {1}", minplayer, mingoals);
            Console.WriteLine("");
            Console.WriteLine("The average goals scored is {0}", average);
            Console.ReadLine();
        }