Doubt about methods

0

I'm learning from the book Use the Head and there is the following code to do an exercise of random items

   public class Menu
        {
        public Random Randomico;
        string[] Carnes = { "Rosbife", "Salame", "Peru", "Presunto", "Pastrami" };
        string[] Condimentos = { "Mostarda amarela", "Mostarda Marrom", "Mostarda com mel", "Maionese", "Molho Francês", "Gosto" };
        string[] Paes = { "Centeio", "Branco", "Trigo", "Pão italiano", "Pão integral", "árabe" };
    }

    public string ItemMenu() {

        string ramdonCarne = Carnes[Randomizer.Next(Carnes.Lenght)];
        string ramdonCondimento = Condimentos[Randomizer.Next(Condimentos.Lenght)];
        string ramdonPao = Paes[Randomizer.Next(Paes.Lenght)];
        return ramdonCarne + "com " + ramdonCondimento + "no " + ramdonPao;
    }

However, in Visual Studio, it is giving error when creating the ItemMenu method, but the book does not tell you anything about something unusual that might happen.

Someone could help me.

    
asked by anonymous 24.04.2018 / 17:00

1 answer

4
public class Menu {
    private static Random randomico = new Random();
    private static string[] Carnes = { "Rosbife", "Salame", "Peru", "Presunto", "Pastrami" };
    private static string[] Condimentos = { "Mostarda amarela", "Mostarda Marrom", "Mostarda com mel", "Maionese", "Molho Francês", "Gosto" };
    private static string[] Paes = { "Centeio", "Branco", "Trigo", "Pão italiano", "Pão integral", "árabe" };

    public string Item() {
        string ramdonCarne = Carnes[randomico.Next(Carnes.Lenght)];
        string ramdonCondimento = Condimentos[randomico.Next(Condimentos.Lenght)];
        string ramdonPao = Paes[randomico.Next(Paes.Lenght)];
        return ramdonCarne + " com " + ramdonCondimento + " no " + ramdonPao;
    }
}

I placed GitHub for future reference .

The method must be within the class. This is the main mistake. But also the use of random was wrong, it lacked to initialize and to use the name of the correct variable.

Do you need to do this? I do not know if it is book orientation or you did not quite understand what you should do, but the code is a bit strange.

    
24.04.2018 / 17:06