gpt4 book ai didi

c# - 开关的返回值始终为 0

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:58 25 4
gpt4 key购买 nike

我正在为学校做一个项目,我正在尝试计算汽车在一定距离内的油耗,但开关始终返回 0。我尝试了不同的方法,但似乎都不起作用。

    public Double DetermineFuelConsumption (String Car, Double Kilometers)
{
Double FuelConsumption = 0;

switch (Car)
{
// (14 / 100) ---> Liters per 100 Kilometers
case "LaFerrari":
FuelConsumption = (14 / 100) * Kilometers;
break;
case "Nissan GT-R Nismo":
FuelConsumption = (12 / 100) * Kilometers;
break;
case "Pagani Huayra":
FuelConsumption = (18 / 100) * Kilometers;
break;
case "McLaren P1":
FuelConsumption = (8 / 100) * Kilometers;
break;
case "Lamborghini Aventador":
FuelConsumption = (16 / 100) * Kilometers;
break;
case "Smart":
FuelConsumption = (4 / 100) * Kilometers;
break;
case "Porsche 911":
FuelConsumption = (9 / 100) * Kilometers;
break;
case "Audi R8":
FuelConsumption = (15 / 100) * Kilometers;
break;
}
return FuelConsumption;
}

最佳答案

所有这些数字除以 100 都会得到 0。

这是整数除法:一个整数除以一个整数的结果总是一个整数。您需要指定您的数字是浮点类型:

public Double DetermineFuelConsumption (String Car, Double Kilometers)
{
Double FuelConsumption = 0;

switch (Car)
{
// (14 / 100) ---> Liters per 100 Kilometers
case "LaFerrari":
FuelConsumption = (14d / 100d) * Kilometers;
break;
case "Nissan GT-R Nismo":
FuelConsumption = (12d / 100d) * Kilometers;
break;
case "Pagani Huayra":
FuelConsumption = (18d / 100d) * Kilometers;
break;
case "McLaren P1":
FuelConsumption = (8d / 100d) * Kilometers;
break;
case "Lamborghini Aventador":
FuelConsumption = (16d / 100d) * Kilometers;
break;
case "Smart":
FuelConsumption = (4d / 100d) * Kilometers;
break;
case "Porsche 911":
FuelConsumption = (9d / 100d) * Kilometers;
break;
case "Audi R8":
FuelConsumption = (15d / 100d) * Kilometers;
break;
}
return FuelConsumption;
}

这里,d 后缀指定这些数字是 double ,而不是整数。

关于c# - 开关的返回值始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35187610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com