Home > database >  Do while loop determined by users input c#
Do while loop determined by users input c#

Time:12-20

I have a lottery game where I am trying to determine the amount of lines the users has entered.E.G They enter 4, they receive 4 sets of 5 random numbers. I am trying to use a do while loop but do not know where to enter the variable in the loop?

        public static void EuroDraw()
        {
            
            Console.WriteLine("How many lines do you wish to purchase (minimum 1):");
            int euroLines = int.Parse(Console.ReadLine());

            do
            {   
                int[] EuroMillions = new int[5];
                Random EuroNumbers = new Random();

                for (int i = 0; i < EuroMillions.Length; i  )
                {
                    EuroMillions[i] = EuroNumbers.Next(1, 50);
                }
                Array.Sort(EuroMillions);

            Console.WriteLine("Your Euro Millions Numbers are:");
            for (int i = 0; i < EuroMillions.Length; i  )
                Console.WriteLine(EuroMillions[i]);
            } while (euroLines <= euroLines);
            Console.ReadLine();

I have tried entering the while condition where the variable needs to equal the varialbe as seen above but it ends in a never ending loop.Any help much appreciated

CodePudding user response:

while (euroLines <= euroLines);

(do you see it yourself?)

...

hint: a variable is always equal to itself, so this never becomes false --> infinite loop

So you need another variable which is increasing in the loop:

int euroLines = int.Parse(Console.ReadLine());
int currentLine = 1;  // <-- this
do
{   
    // ...
} while (  currentLine <= euroLines);
  • Related