Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 83k

how to use a loop in this case?

Aug 11 2022 3:38 PM

Hi,

I want to show the three players in a loop instead of using 3 same lines. I tried something like this but it doesn't work.

Thanks for help.

V

class Tennis
{
    public string name;
}

class Head

        static void Main(string[] args)
        {
        Tennis player1 = new Tennis();
        player1.name = "Nadal";
    Tennis player2 = new Tennis();
        player2.name = "Djokovic";
    Tennis player3 = new Tennis();
        player3.name = "Federer";

for (int i=1;i<=3;i++)
            {
                Console.WriteLine((player+i).name);
            }
    }
}


Answers (3)