2、编写一个JAVA应用程序,该程序可以按照包裹的重量计算从北京到上海的快递费用。快递费用的算法如下:不到1公斤,快递费用10元。超过1公斤不到6公斤,每超过1公斤8元,不足1公斤按照1公斤计算。
时间: 2024-05-10 21:21:03 浏览: 29
```java
import java.util.Scanner;
public class ExpressDelivery {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入包裹重量(单位:千克):");
double weight = scanner.nextDouble();
double cost; // 快递费用
if (weight <= 1) {
cost = 10;
} else if (weight <= 6) {
int num = (int) Math.ceil(weight - 1); // 超过1公斤的部分向上取整
cost = 10 + num * 8;
} else {
int num = (int) Math.ceil(weight - 1); // 超过1公斤的部分向上取整
cost = 10 + (5 * 8) + num * 10;
}
System.out.println("快递费用:" + cost + "元");
}
}
```