public static void calculateParkingFee(double hours) { double fee; if (hours <= 3) { fee = 5; } else if (hours >= 20) { fee = 20; } else { fee = 5 + (hours - 3) * 1; fee = Math.min(fee, 20); // 最高收费不超过20元 } System.out.println("停车费用为:" + fee + "元"); }结果四舍五入取10,完善代码
时间: 2023-06-07 08:07:38 浏览: 62
如何优雅的替换掉Java代码中的if else
5星 · 资源好评率100%
public static void calculateParkingFee(double hours) {
double fee;
if (hours <= 3) {
fee = 5;
} else if (hours >= 20) {
fee = 20;
} else {
fee = 5 + (hours - 3) * 1;
fee = Math.min(fee, 20); // 最高收费不超过20元
}
fee = Math.round(fee / 10) * 10; // 四舍五入取10
System.out.println("停车费用为:" + fee + "元");
}
阅读全文