none
How to show the name of the winner and how many points he got? RRS feed

  • Question

  • This is a console applications program. 

    I have a problem with showing the winner of this tournament. I want to show winners name and how many points he got but at the moment it only shows the winners points. What do I need to do?

    This is the cod  

    string[] deltagare = new string[20];
                deltagare[0] = "Peter";
                deltagare[1] = "Daniel";
                deltagare[2] = "Kim";
                deltagare[3] = "Philip";
                deltagare[4] = "Arild";
                deltagare[5] = "Junas";
                deltagare[6] = "Hans";
                deltagare[7] = "Hugo";
                deltagare[8] = "Linus";
                deltagare[9] = "Simon";
                deltagare[10] = "Johan";
                deltagare[11] = "Sigurd";
                deltagare[12] = "Robert";
                deltagare[13] = "Karl";
                deltagare[14] = "Erik";
                deltagare[15] = "Martin";
                deltagare[16] = "Isac";
                deltagare[17] = "Isak";
                deltagare[18] = "Pakorn";
                deltagare[19] = "Nils";

                int[] tävlande = new int[20];
                tävlande[0] = 0;
                tävlande[1] = 0;
                tävlande[2] = 0;
                tävlande[3] = 0;
                tävlande[4] = 0;
                tävlande[5] = 0;
                tävlande[6] = 0;
                tävlande[7] = 0;
                tävlande[8] = 0;
                tävlande[9] = 0;
                tävlande[10] = 0;
                tävlande[11] = 0;
                tävlande[12] = 0;
                tävlande[13] = 0;
                tävlande[14] = 0;
                tävlande[15] = 0;
                tävlande[16] = 0;
                tävlande[17] = 0;
                tävlande[18] = 0;
                tävlande[19] = 0;

                Console.WriteLine("\n De tävlande fick följande poäng: \n");
                Random rnd = new Random();

                for ( int x= 0; x < 20; x++)
                {
                    for (int i = 0; i < 20; i++)
                    {

                        int p = rnd.Next(3);

                        if (p == 0)
                            tävlande[x] += 20;
                        else if (p == 1)
                            tävlande[x] += 10;
                    }
                    Console.WriteLine("\n" + deltagare[x]);
                    Console.WriteLine(tävlande[x]);



                }
                int a = tävlande.Max();

                Console.WriteLine("Vinnaren var: vinnaren med " + a + "p");
            }

                                                
    Saturday, November 4, 2017 12:31 PM

All replies

  • Hi,

    please refer below.

    int mount= 20; //the number of participate
    
    string[] deltagare = new string[mount];
                deltagare[0] = "Peter";
                deltagare[1] = "Daniel";
                deltagare[2] = "Kim";
                deltagare[3] = "Philip";
                deltagare[4] = "Arild";
                deltagare[5] = "Junas";
                deltagare[6] = "Hans";
                deltagare[7] = "Hugo";
                deltagare[8] = "Linus";
                deltagare[9] = "Simon";
                deltagare[10] = "Johan";
                deltagare[11] = "Sigurd";
                deltagare[12] = "Robert";
                deltagare[13] = "Karl";
                deltagare[14] = "Erik";
                deltagare[15] = "Martin";
                deltagare[16] = "Isac";
                deltagare[17] = "Isak";
                deltagare[18] = "Pakorn";
                deltagare[19] = "Nils";
    
                int[] tävlande = new int[mount];
                tävlande[0] = 0;
                tävlande[1] = 0;
                tävlande[2] = 0;
                tävlande[3] = 0;
                tävlande[4] = 0;
                tävlande[5] = 0;
                tävlande[6] = 0;
                tävlande[7] = 0;
                tävlande[8] = 0;
                tävlande[9] = 0;
                tävlande[10] = 0;
                tävlande[11] = 0;
                tävlande[12] = 0;
                tävlande[13] = 0;
                tävlande[14] = 0;
                tävlande[15] = 0;
                tävlande[16] = 0;
                tävlande[17] = 0;
                tävlande[18] = 0;
                tävlande[19] = 0;
    
     Console.WriteLine("\n De tävlande fick följande poäng: \n");
     Random rnd = new Random();
    
     for ( int x= 0; x < mount; x++)
     {
        for (int i = 0; i < mount; i++)
        {
    
         int p = rnd.Next(3);
    
          if (p == 0)
             tävlande[x] += 20;
          else if (p == 1)
             tävlande[x] += 10;
         }
     Console.WriteLine("\n" + deltagare[x]);
     Console.WriteLine(tävlande[x]);
     }
    int maxPoint = tävlande.Max();
    int maxIndex=0;
    
    for(int i=0;i<mount;i++)//this is only for one winner in the game. if there is more than one winner, we may optimize the code.
    {
     if(maxPoint==tävlande[i]) 
       maxIndex=i;
    }
     Console.WriteLine("Vinnaren var: vinnaren med " + deltagare[i] + tävlande[i] );
    }

    Saturday, November 4, 2017 4:20 PM
  • This part of the code does not work, it says that i does not consist in the current contest

     

    Console.WriteLine("Vinnaren var: vinnaren med " + deltagare[i] + tävlande[i] );
    Saturday, November 4, 2017 5:42 PM
  • Create a class containing a Name and a Score property, instead of two different arrays.  Then use a List of that class to store the information.

    public class Player
    {
        public string Name { get; set; }
        public int Score { get; set; }
        
        public Player (string name, int score)
        {
            this.Name = name;
            this.Score = score;
        }
    }

    Initialize the list like this:

    List<Player> Players = new List<Player>();
    Players.Add(new Player("Peter", 0);
    Players.Add(new Player("Daniel", 0);
    Players.Add(new Player("Kim", 0);
    // and so on for the rest of them
    
    
    
    for ( int x= 0; x < Players.Count; x++)
    {
        int p = rnd.Next(3);
    
        if (p == 0)
         Players[x].Score += 20;
        else if (p == 1)
         Players[x].Score += 10;
    }    
    

    To find the winner:

    Player winner = Players.Where(s => s.Score == Players.Max(p => p.Score)).FirstOrDefault();
    Console.WriteLine("Winner is {0} with a score of {1}", winner.Name, winner.Score);

    ...or winners if there are more than one with the same high score:

    List<Player> winners = Players.Where(s => s.Score == Players.Max(p => p.Score)).Select(p => p).ToList();


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, November 5, 2017 5:25 PM
    Moderator
  • Hello Dacke2000,

    If you don't want to modify a lot in your project, you could quickly fix it by below.

           //Your original code
    
                int a = tävlande.Max();
    
                var index = Array.FindIndex(tävlande, score => score == a);
    
                Console.WriteLine("Vinnaren  var: vinnaren med " + a + "p" +" \this Name is {0}", deltagare[index]);
    
                Console.ReadLine();

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 6, 2017 9:26 AM
    Moderator
  • Hello ,

    Is there anything updated? If the issues has been solved, you could close the thread by marking  as answer, This can be beneficial to other community members reading this thread.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 15, 2017 10:52 AM
    Moderator