Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Do while loop determined by users input c#

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading