Valerie Meunier

Valerie Meunier

  • 954
  • 693
  • 72.5k

how to get the position of maximum value in class array?

Dec 22 2022 2:46 PM

Hi

I dont' know how to get the position of the maximum score (line in bold). Thanks for help.

using System;
using System.Linq;
class Footbal_oop
{
    public static void Main()
    {
        int match;
        Random rand = new Random();
        TeamScore[] teams = new TeamScore[] { new TeamScore("A",0), new TeamScore("B",0), new TeamScore("C",0) };

        for (int i = 0; i <= teams.Length - 1; i++)
        {
            for (int j = i + 1; j <= teams.Length - 1; j++)
            {
                match = rand.Next(1, 4); // 1 = team A wins, 2 = team B wins, 3 = even
                switch (match)
                {
                    case 1:
                        teams[i].Score += 3;
                        Console.WriteLine(teams[i].TeamName + " wins against " + teams[j].TeamName);
                        Console.WriteLine();
                        break;
                    case 2:
                        teams[j].Score += 3;
                        Console.WriteLine(teams[i].TeamName + " loses against" + teams[j].TeamName);
                        Console.WriteLine();
                        break;
                    default:
                        teams[i].Score += 1;
                        teams[j].Score += 1;
                        Console.WriteLine(teams[i].TeamName + " even " + teams[j].TeamName);
                        Console.WriteLine();
                        break;
                }
            }
            Console.ReadLine();
        }
        int m = teams.Max(t => t.Score);
        int p = Array.IndexOf(teams, m); // doesn't work I need the position of the maximum value of Score
                
        foreach (TeamScore i in teams)
            Console.WriteLine(i);
        Console.WriteLine();
    }
}

class TeamScore
{
    public string TeamName { get; set; }
    public int Score { get; set; }

    public TeamScore(string name, int score) // constructor
    {
        TeamName = name;
        Score = score;
    }
 


Answers (3)