C# 此方法用于打印停车场中超过两小时的每小时的不同值,但它打印的是最大值
此方法的目的是计算和显示车辆的停车费 每个在这个车库里停了一辆车的顾客,但它只是打印出来的 对于任何花费超过3小时的客户,最大价值为10美元C# 此方法用于打印停车场中超过两小时的每小时的不同值,但它打印的是最大值,c#,C#,此方法的目的是计算和显示车辆的停车费 每个在这个车库里停了一辆车的顾客,但它只是打印出来的 对于任何花费超过3小时的客户,最大价值为10美元 double CalculateCharges(double time) //method header { if (time >= 0 && time <= 3) //returns 2 if the individual spends 3 or less hours //in the
double CalculateCharges(double time) //method header
{
if (time >= 0 && time <= 3)
//returns 2 if the individual spends 3 or less hours
//in the car park
return 2.0;
else if (time > 3 && time <= 24) // if time is greater than 3 hrs then do this
{
double basic = 2.0; // basic charge to a customer
for (time = 4; time <= 19; time++)
{
// basic charge + 0.5 for every extra hour over 3 hrs
basic += 0.5;
}
return basic;
}
else
//if the person spends more than 19 hrs lets the
// person pay a maximum of 10 dollars
return 10;
}
double CalculateCharges(双倍时间)//方法头
{
如果(time>=0&&time 3&&time你的for
循环将始终计算相同的值,因为你总是将时间从4计数到19。最好将其更改为这样,添加一个新的控制变量i
:
double basic = 2.0;
for (int i = 4; i <= Math.Ceiling(time); i++)
{
basic += 0.5;
}
我知道您的方法使用double作为参数,但请允许我使用Timespan提供一个替代方法,在这种情况下,它更合适:
public static double CalculateCharges(TimeSpan time)
{
if (time < TimeSpan.Zero)
{
throw new ArgumentException("Parking time cannot be less than 0", nameof(time));
}
const double initialPrice = 2.0;
const double maxPrice = 10.0;
if (time <= TimeSpan.FromHours(3))
{
return initialPrice;
}
if (time >= TimeSpan.FromHours(19))
{
return maxPrice;
}
// ignore the first 3 hours
int hours = time.Hours - 3;
// if time is 1 hour and 5 minutes we should charge 2 hours
// No need to check for seconds, I suppose :)
if (time.Minutes > 0)
{
hours++;
}
double finalPrice = initialPrice + (hours * 0.5);
return finalPrice;
}
输出:
出于好奇,为什么不使用时间跨度而不是双参数?@RuiJarimba我还没有达到这个水平,但我会做一些研究,比如开关(案例)对于24个案例来说,这个陈述可能也值得研究一下@ITAlex@ITAlex在这个特殊的情况下,我不认为交换语句会有很大帮助。如果<代码>时间<代码>有小数部分,例如“代码> 4.50”/代码>,则不能正常工作。考虑使用以获得大于或等于指定的十进制数的最小整数值。ryou还需要在循环中完成;-)
public static double CalculateCharges(TimeSpan time)
{
if (time < TimeSpan.Zero)
{
throw new ArgumentException("Parking time cannot be less than 0", nameof(time));
}
const double initialPrice = 2.0;
const double maxPrice = 10.0;
if (time <= TimeSpan.FromHours(3))
{
return initialPrice;
}
if (time >= TimeSpan.FromHours(19))
{
return maxPrice;
}
// ignore the first 3 hours
int hours = time.Hours - 3;
// if time is 1 hour and 5 minutes we should charge 2 hours
// No need to check for seconds, I suppose :)
if (time.Minutes > 0)
{
hours++;
}
double finalPrice = initialPrice + (hours * 0.5);
return finalPrice;
}
TimeSpan twentyMinutes = TimeSpan.FromMinutes(20);
TimeSpan twoHours = TimeSpan.FromHours(2);
TimeSpan threeHours = TimeSpan.FromHours(3);
TimeSpan threeHoursAndAHalf = new TimeSpan(3, 30, 0);
TimeSpan fiveHours = TimeSpan.FromHours(5);
TimeSpan tenHours = TimeSpan.FromHours(10);
TimeSpan nineteenHours = TimeSpan.FromHours(19);
TimeSpan twentyHours = TimeSpan.FromHours(20);
Console.WriteLine($"CalculateCharges({nameof(twentyMinutes)}) = {CalculateCharges(twentyMinutes):C}");
Console.WriteLine($"CalculateCharges({nameof(twoHours)}) = {CalculateCharges(twoHours):C}");
Console.WriteLine($"CalculateCharges({nameof(threeHours)}) = {CalculateCharges(threeHours):C}");
Console.WriteLine($"CalculateCharges({nameof(threeHoursAndAHalf)}) = {CalculateCharges(threeHoursAndAHalf):C}");
Console.WriteLine($"CalculateCharges({nameof(fiveHours)}) = {CalculateCharges(fiveHours):C}");
Console.WriteLine($"CalculateCharges({nameof(tenHours)}) = {CalculateCharges(tenHours):C}");
Console.WriteLine($"CalculateCharges({nameof(nineteenHours)}) = {CalculateCharges(nineteenHours):C}");
Console.WriteLine($"CalculateCharges({nameof(twentyHours)}) = {CalculateCharges(twentyHours):C}");