【问题标题】:How do I get this WHILE loop to work?如何让这个 WHILE 循环工作?
【发布时间】:2016-09-27 01:03:40
【问题描述】:

目前正在做一个我认为我做对了的练习,使用了 IF ELSE 语句,但是一旦提交,我被告知要使用 WHILE 或 DO-WHILE 循环,我遇到了一些麻烦。你会看到我在哪里使用了 WHILE 循环,但它给了我分配给它的错误消息的无限循环,我不知道如何让它停止!

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();
    }
}

所以我的想法是我希望“输入正确的小时数 - 1 到 24。”如果用户输入的数字小于 1 或大于 24,则显示,否则程序继续使用 IF和 ELSE 语句。

【问题讨论】:

  • ReadKey 在循环内,检查输入的 char 为数字和 1-24 之间。如果是,break.
  • 您需要允许用户更正无效条目。在 while 循环中放置另一个 Console.ReadLine() 以收集新版本的 hours,否则您将在第一个错误时永远循环。
  • 谢谢约翰。我按照下面的答案添加了代码,但我遇到了一个新问题:你提供的这个 while 循环创造了奇迹,直到某一点。当我输入错误的小时数时,比如说 25,然后它要求我输入正确的小时数,此时任何输入都会提供 MAX_FEE(20 美元)的答案。有办法解决吗? ——

标签: c# loops if-statement while-loop do-while


【解决方案1】:

你忘记再次读取while循环中的值以获取新值:

    while (hours < 1 || hours > 24) 
    {
        Console.Write("Enter correct amount of hours - 1 to 24. ");
        Console.WriteLine("Enter the amount of hours parked:");
        hours = decimal.Parse(Console.ReadLine());
    }

或者作为一个 Do While 你可以摆脱第一次读取并且只在错误时循环

do {
   Console.WriteLine("Enter the amount of hours parked:");
   try{
     hours = decimal.Parse(Console.ReadLine());
     if(hours <1 || hours > 24) 
        Console.Write("Enter correct amount of hours - 1 to 24. ");
   } 
   catch { Console.WriteLine("Please enter a valid numeric value"); }
} while(hours < 1 || hours > 24);

我添加了 try catch,因为如果他们输入一个非数字值,它可能会引发错误。

【讨论】:

  • 你提供的这个while循环创造了奇迹,直到某一点。当我输入错误的小时数时,假设是 25,然后它要求我输入正确的小时数,此时任何输入都会提供 MAX_FEE(20 美元)的答案。有办法解决吗?
  • @LegendIsReal 你应该在解析hours 后计算你的parkingCost。将decimal parkingCost = hours * HOURLY_RATE;“之后”和“外部”移动到do-while 循环
  • 正是保罗所说的。在 while 循环之后移动计算
【解决方案2】:

只需在while循环中添加接受输入的行,如下所示

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. ");
        hours = decimal.Parse(Console.ReadLine());
    }


    parkingCost = hours * HOURLY_RATE; // to recalculate

    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();
}

}

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 2021-12-26
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多