运输公司计算运费功能java用switch
时间: 2024-09-19 11:15:43 浏览: 83
在Java中,你可以使用`switch`语句来创建一个简单的运输公司运费计算功能,比如基于货物的种类、重量或者目的地来计算不同的费用。这里是一个简化的例子:
```java
public class ShippingCompany {
public static void main(String[] args) {
String item = "books"; // 货物类型
double weight = 50; // 货物重量 单位:千克
String destination = "domestic"; // 国内或国际目的地
double baseFee = 0;
switch (destination) {
case "domestic":
switch (item) {
case "books":
if (weight <= 10) {
baseFee = 10;
} else if (weight > 10 && weight <= 20) {
baseFee = 20;
} else {
baseFee = 30;
}
break;
// 添加其他物品类型的分支...
}
break;
case "international":
// 根据不同的国家设定不同的费用规则...
break;
}
System.out.println("运费:$" + baseFee);
}
}
```
在这个示例中,我们首先根据目的地选择国内还是国际运费计算,然后在每个目的地分支内部,再依据货物类型(如书籍)和重量计算具体的费用。当然,实际应用中可能需要更复杂的条件判断和更多的货物类型。
阅读全文
相关推荐












