hassy786 hassy786 - 1 year ago 87
C# Question

If statement or loops?

Very new to c# and would a little guideance as to what I should follow with this step as I am stuck :(

It may be simple to answer for you guys but I have spent the last 1 hour trying to find what I need but cannot seem to get it working...

My question is.. A customer gets to enter the values 1,2,3,4,5 or 6 but there was nothing stopping them from entering a number below 1 or above 6 therefore I had introduced a if statement which says....

decimal theirchoice = decimal.Parse(Console.ReadLine());
if (theirchoice < 1 || theirchoice > 6)
Console.WriteLine("You must enter a number between 1 and 6.");

However now I need a code that when the customer presses enter.. it will ask them to enter a value again so this time they can enter a value between 1 and 6.. I am not sure if I should use a loop or another if statement for this and how to use them :/

Any help would be much appreciated.. I dont want the answer! This is for my assignment.. Just need to know what I should be using and a video or something to how I use it.

Thank You.

Answer Source

To follow Steve's piece of advice, you could use this:

    int theirchoice;
        Console.WriteLine("enter a number between 1 and 6");
    } while (!Int32.TryParse(Console.ReadLine(), out theirchoice) || (theirchoice < 1 || theirchoice > 6));