Random in the switch statement, why does not it work?

1

Following the code below, my intention was to randomize the switch to choose between case 1 and 2 after receiving the word "gamble". It turns out that when I type gamble, only case 1 (add 10 irons) is activated, instead of being random between adding 10 or subtracting 10. What did I do wrong?

            int ferros;          
            string gamble;
            gamble = Console.ReadLine();
            Random rnd = new Random();
            int luck = rnd.Next(1,3);
            bool loop = true;
            while (loop)
            {
              if (gamble == "gamble")
              {
                  switch (luck)
                  {
                    case 1:
                        ferros = ferros + 10;
                        break;
                    case 2:
                        ferros = ferros - 10;
                        break;
                  }
              }
            }
    
asked by anonymous 30.08.2018 / 05:04

1 answer

3

The error was not having entered the "system" to randomize within the loop. The resolution to the problem is to implement the gamble = Console.ReadLine (); and the variable int luck = rnd.Next (1,3); within the loop, so that the "system" of randomization is always repeated.

        int ferros;          
        string gamble;
        Random rnd = new Random();
        bool loop = true;
        while (loop)
        {
         gamble = Console.ReadLine();
         int luck = rnd.Next(1,3);


            if (gamble == "gamble")
            {
              switch (luck)
              {
                case 1:
                    ferros = ferros + 10;
                    break;
                case 2:
                    ferros = ferros - 10;
                    break;
              }
            }
        } //Code
    
30.08.2018 / 06:42