在停车计算器中验证1-24之间的值

时间:2018-04-04 06:09:15

标签: c#

所以我有一个作业,我需要验证从键盘输入的数字在1-24之间。计算器的代码如下,很简单。它输入的值乘以2.5乘以最大值20.我需要添加代码以确保输入的数字在1-24之间。请帮忙。我想我需要另一个if / else语句。

/ * Harrison Currie              * 24/03              *转让停车费1              *伪代码              *每小时费率= 2.50              *停车费=小时费*              * MAX FEE = 20.00              *输出总成本最高为20美元              *验证时间在1-24之间              * /

        //Set Constants
        const decimal HOURLY_RATE = 2.5m;
        const decimal MAX_FEE = 20.00m;
        //Declare Variables
        decimal PARKING_FEE;
        decimal HOURS;
        //Input
        //Enter HOURS as a decimal

        PARKING_FEE = HOURS * HOURLY_RATE;


        bool valid = false;
        while (!valid)
        {
            Console.WriteLine("Enter Number Of Hours Parked");
            HOURS = int.Parse(Console.ReadLine());
            if (HOURS > 0 && HOURS <= 24)
            {
                valid = true;
            }
            else
            {
                Console.WriteLine("Hours must be between 1-24");
            }

            if (PARKING_FEE >= 20.00m)
            {
                PARKING_FEE = MAX_FEE;
            }
            else
            {
                PARKING_FEE = HOURS * HOURLY_RATE;
            }
            //Output
            Console.WriteLine("Developed By Harrison Currie");
            Console.WriteLine("The Cost Of Your Park Is $" + PARKING_FEE);
            Console.Read();

1 个答案:

答案 0 :(得分:0)

添加,并检查输入是否在范围内。还有一些信息消息。 类似的东西:

   /* Harrison Currie
                * 24/03
                * Assignment Parking Fee 1
                * Pseudocode
                * HOURLY RATE = 2.50
                * PARKING FEE = HOURS *Fee
                * MAX FEE = 20.00
                * OUTPUT TOTAL COST TO A MAX OF $20
                * Validate Hours are between 1-24
                */

        //Set Constants
        const decimal HOURLY_RATE = 2.5m;
        const decimal MAX_FEE = 20.00m;
        //Declare Variables
        decimal PARKING_FEE;
        decimal HOURS =0;
        //Input
        bool valid = false;

        while (!valid)
        {
            Console.WriteLine("Enter hours of parking: ");
         bool parse =  decimal.TryParse(Console.ReadLine(),out HOURS);
            if (!parse)
            {
                Console.WriteLine("Not a number.");
                continue;
            }

            if(HOURS > 0 && HOURS <= 24)
            {
                valid = true;
            }
            else
            {
                Console.WriteLine("Hours must be between 0-24");
            }
        }
        PARKING_FEE = HOURS * HOURLY_RATE;


        if (PARKING_FEE >= 20.00m) PARKING_FEE = MAX_FEE;
        else PARKING_FEE = HOURS * HOURLY_RATE;
        //Output
        Console.WriteLine("Developed By Harrison Currie");
        Console.WriteLine("The Cost Of Your Park Is $" + PARKING_FEE);
        Console.Read();