首先,我想对这个网站表示感谢;当我需要调试时,它可以帮助我多次走上正确的轨道。
我对C#非常满意;我是编程入门学生。我编写了一个程序来计算每个人的停车费,然后对所有停车费进行总计。按照书中的示例,我设置了一个“ While”循环,仅在数据输入人员键入“ -1”时结束,否则,将不断要求他们输入其他停车费用。
我在获取所有人的总费用时遇到了麻烦,并联系了我的教授。他帮助我解决了这个问题,但又提出了另一个问题,却没有意识到。我不愿意第二次回到井口,尤其是。指出他创造了一个新问题。问题是当您键入-1以停止循环时,它将返回合计应付款。它不应该那样做,只是在那里停止循环。另外,总数甚至不准确。它具有先前条目的价值(这太奇怪了,对我来说似乎是一个重要线索)。请注意,循环确实在这一点停止,这样很好。它只是不应该还退还停车费。
下面是大多数代码(我省略了变量的初始化/声明,但是可以根据需要添加)。我觉得While和If语句应该完全阻止“ -1”的计算;无法弄清楚它如何“通过”:
while (hoursParked != -1)
{
Console.WriteLine("Enter each customer's total hours parked (no more than 24) or type -1 to quit: ");
hoursParked = Double.Parse(Console.ReadLine());
if (hoursParked > 0 && hoursParked < 3.0)
{
feeCharged = lessThan3Rate;
}
if (hoursParked >= 3.0 && hoursParked <= 18.00)
{
moreThan3thru18Rate = (2.00 + ((hoursParked - 2) * 0.50));
feeCharged = moreThan3thru18Rate;
if (hoursParked > 18.00)
{
feeCharged = moreThan18Rate;
}
Console.WriteLine($"The customer's charge is: {feeCharged:C}");
Console.WriteLine();
totalAllReceipts = totalAllReceipts + feeCharged;//This will be the sum of all the fees charged.
}
{
Console.WriteLine("End of Day Totals:");
Console.WriteLine($"The daily total of all receipts is: {totalAllReceipts:C}");
}
我肯定有更有效的方法来执行计算,但是这是我们在课堂上就概念而言的位置,因此请忽略补救级别的代码! TIA寻求任何帮助!
while
条件仅在循环的顶部进行评估。因此,如果输入-1,则剩余的lop仍将执行。然后,它评估while
条件tand then存在,计算总数。
一个选项是改用while(true)
,如果输入为-1,则中断:
while (true)
{
Console.WriteLine("Enter each customer's total hours parked (no more than 24) or type -1 to quit: ");
hoursParked = Double.Parse(Console.ReadLine());
if(hoursParked == -1) break;
if (hoursParked > 0 && hoursParked < 3.0)
{
feeCharged = lessThan3Rate;
}
尝试如果(hoursParked!=-1)totalAllReceipts = totalAllReceipts +费用已收费;