samedi 28 mai 2016

How do I get this WHILE loop to work?

Currently doing an exercise that I thought I had done right, using an IF ELSE statement, but once submitted I was told instead to use a WHILE or DO-WHILE loop, and I'm having a bit of trouble. You'll see where I've used the WHILE loop, but it is giving me an infinite loop of the error message that I assigned to it and I don't know how to make it stop!

static void Main(string[] args)
    {
        decimal hours;
        const decimal HOURLY_RATE = 2.5m;
        const decimal MAX_FEE = 20.00m;
        Console.WriteLine("Enter the amount of hours parked:");
        hours = decimal.Parse(Console.ReadLine());
        decimal parkingCost = hours * HOURLY_RATE;

        while (hours < 1 || hours > 24) 
        {
            Console.Write("Enter correct amount of hours - 1 to 24. ");
        }

        if (parkingCost > MAX_FEE) 
        {
            Console.Write("Total fee is $" + MAX_FEE);
            Console.WriteLine(" Time parked in hours is " + hours);
        }
        else
        {
            parkingCost = hours * HOURLY_RATE;
            Console.WriteLine("The cost of parking is " + parkingCost.ToString("C"));
            Console.WriteLine("Time parked in hours is " + hours);
        }
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
}

So the idea is that I want for the "Enter correct amount of hours - 1 to 24. " to display if the user enters a number less than 1 or greater than 24, otherwise for the program to go ahead with the IF and ELSE statements.

Aucun commentaire:

Enregistrer un commentaire