Doubt about methods

Asked

Viewed 49 times

0

I am learning by the book Use the Head and in it 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, is giving error when creating the Itemmenu method, but the book does not inform anything about something unusual that may happen.

Could someone help me.

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 put in the Github for future reference.

The method needs to be within the class. This is the main error. But also the use of random was wrong, missing initialize and use the name of the correct variable.

Does it need to do so? I don’t know if it’s book orientation or not quite understood what I should do, but the code is a little strange.

  • You’re right, man. I tidied it up here and it was straight. The main problem is that I am following an English workbook and translating at the same time, so the name of some variables are wrong, because I forget to translate. But the main error was that I left the method out of the class. Thanks a lot for the help, Maniero. Really worth it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.