如果输入无效数据,如何重复代码

时间:2018-08-29 05:36:11

标签: c# repeat

我需要使计算停车费的代码出现错误,并要求用户重新输入数据(如果提供了错误的数字)。例如,如果用户输入的金额小于1或大于24,则将显示错误代码,并要求用户重新输入有效金额。输入有效金额后,我希望它输出parkFee。 我还没有更新我的伪代码,对此深表歉意。

    /* PSEUDOCODE */
    /* HOURLY_RATE=2.5
     * INPUT parkTime
     * parkFee = HOURLY_RATE * hours
     * OUTPUT parkFee */


        decimal parkTime;  // input - time in hour eg 1.5 for 1 and a half hours 
        const decimal HOURLY_RATE = 2.50m; // HOURLY_RATE * INPUT parkTime = parkFee
        const decimal MAX_FEE = 20.00m; // MAX_FEE is set as a payment cap and ignores any extra charges incurred over 8 hours
        decimal parkFee;
        Console.WriteLine("ParkingFee1 program developed by: Ryley Copeman");
        Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");
        parkTime = decimal.Parse(Console.ReadLine());

        if (parkTime > 8)
        {
            Console.Write("Total fee is $" + MAX_FEE);
        }
        else
        {
            parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;
            Console.Write("Parking Fee = $" + parkFee);
        }
        while(parkTime < 0 || parkTime > 24)    // validate... 
        //while (parkTime <= 0) )
        {
        Console.WriteLine("Error – Park Time out of range");
        Console.WriteLine("Enter - Park Time between 0 and 24 (HOURS):");
        parkTime = int.Parse(Console.ReadLine());

    }

    }
}

}

4 个答案:

答案 0 :(得分:6)

我认为您只需要这个:

do
{
    Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");
    parkTime = decimal.Parse(Console.ReadLine());

    if (parkTime < 1 || parkTime > 24)
    {
        Console.WriteLine("Error – Park Time out of range");
    }
}
while (parkTime < 1 || parkTime > 24);

if (parkTime > 8)
{
    Console.Write("Total fee is $" + MAX_FEE);
}
else
{
     parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;
     Console.Write("Parking Fee = $" + parkFee);
 }

请注意,您可能需要调整代码以始终计算费用,然后应用最大值:

parkFee = Math.Min(MAX_FEE, Math.Ceiling(parkTime) * HOURLY_RATE);
Console.Write("Parking Fee = $" + parkFee);

这里Math.Min将选择两个值中的最小值。

最后,请注意,如果您输入了意外的内容(例如“ 1.2Hello”或“”),decimal.Parse将会出错,因此最好使用TryParse

bool isValidTime = false;
do
{
    Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");
    bool parsedOK = decimal.TryParse(Console.ReadLine(), out parkTime);
    isValidTime = parsedOK && parkTime >= 1 && parkTime <= 24;
    if (!isValidTime)
    {
        Console.WriteLine("Error – Park Time out of range");
    }
}
while (!isValidTime);

parkFee = Math.Min(MAX_FEE, Math.Ceiling(parkTime) * HOURLY_RATE);
Console.Write("Parking Fee = $" + parkFee);

此处,循环将继续,直到输入有效值为止。请注意,在循环结构中,您还可以使用break;(离开循环)和continue;(移至循环的下一个迭代)来控制流程。

答案 1 :(得分:1)

您已经了解了循环的概念,有几种可能,简单的一种是使用break的循环。

for (;;) {
  // input
  if (condition ok)
    break;
  // output "wrong, try again"
}

这将根据需要重复输入的次数,并在输入合适的值后退出循环。

答案 2 :(得分:1)

 /* PSEUDOCODE */
    /* HOURLY_RATE=2.5
     * INPUT parkTime
     * parkFee = HOURLY_RATE * hours
     * OUTPUT parkFee */

        bool mustRepeat = true;
        decimal parkTime = 0;  // input - time in hour eg 1.5 for 1 and a half hours 
        const decimal HOURLY_RATE = 2.50m; // HOURLY_RATE * INPUT parkTime = parkFee
        const decimal MAX_FEE = 20.00m; // MAX_FEE is set as a payment cap and ignores any extra charges incurred over 8 hours
        decimal parkFee;
        Console.WriteLine("ParkingFee1 program developed by: Ryley Copeman");
        Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");

        while(mustRepeat)    // validate... 
        {
            parkTime = decimal.Parse(Console.ReadLine());

            if(parkTime < 1 || parkTime > 24)
            {       
                Console.WriteLine("Error – Park Time out of range");
                Console.WriteLine("Enter - Park Time between 0 and 24 (HOURS):");
                continue;
            }

            mustRepeat = false;

            if (parkTime > 8)
            {
                Console.Write("Total fee is $" + MAX_FEE);
                break;
            }
            else
            {
                parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;
                Console.Write("Parking Fee = $" + parkFee);
                break;
            }
        }
    }
}

答案 3 :(得分:1)

Console.WriteLine("ParkingFee1 program developed by: Ryley Copeman");
Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");
parkTime = decimal.Parse(Console.ReadLine());
do
{
    if(parkTime < 1 || parkTime > 24)
    {
        Console.WriteLine("Error – Park Time out of range");
        Console.WriteLine("Enter - Park Time between 0 and 24 (HOURS):");
        parkTime = decimal.Parse(Console.ReadLine());
        continue;
    }
    if (parkTime > 8)
    {
        Console.Write("Total fee is $" + MAX_FEE);
    }
    else
    {
        parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;
        Console.Write("Parking Fee = $" + parkFee);
    }
} while(parkTime < 1 || parkTime > 24);