package Demo;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class Demo28 {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int stationsCount;
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "朱辛庄");
map.put(2, "育知路");
map.put(3, "平西府");
map.put(4, "回龙观东大街");
map.put(5, "霍营");
map.put(6, "育新");
map.put(7, "西小口");
map.put(8, "永泰庄");
map.put(9, "林萃桥");
map.put(10, "森林公园南门");
map.put(11, "奥林匹克公园");
map.put(12, "奥体中心");
map.put(13, "北土城");
map.put(14, "安华桥");
map.put(15, "安德里北街");
map.put(16, "鼓楼大街");
map.put(17, "什刹海");
map.put(18, "南锣鼓巷");
map.put(19, "中国美术馆");
stationsCount=getOnOff(map);
spend(stationsCount);
}
private static void spend(int stationsCount) {
System.out.println("你一共坐了"+stationsCount+"站");
int spending = 0;
if (stationsCount==0){
System.out.println("不用付钱,爬");
} else if (stationsCount <= 3) {
spending = 3;
System.out.println("三站内,费用一律为" + spending + "元");
} else if (stationsCount > 3 && stationsCount <= 5) {
spending = 4;
System.out.println("3站以上但不超过5站(包含5站)的收费为" + spending + "元");
} else if (stationsCount > 5) {
spending = 4;
spending += stationsCount * 2;
if (spending >= 10) {
spending = 10;
}
System.out.println("5站以上的费用为" + spending + "元");
} else {
System.out.println("输入错误");
}
}
private static int getOnOff(HashMap<Integer, String> map) {
int stationCount = 0;
int station1Count = 0;
int station2Count=0;
Set<Integer> keys = map.keySet();
while (true) {
System.out.println("请输入上车站");
String station1 = input.next();
if (map.containsValue(station1)) {
for (int key : keys) {
if (map.get(key).equals(station1)) {
station1Count = key;
System.out.println("你已经确定在第" + key + "站:" + station1 + "上车");
}
}
break;
} else {
System.out.println("没有该站,请重新输入你要上车的站");
}
}
while (true) {
System.out.println("请输入要下车站");
String station2 = input.next();
if (map.containsValue(station2)) {
for (int key:keys){
if (map.get(key).equals(station2)){
station2Count=key;
System.out.println("你已经确定在第" + key+"站:"+station2 + "下车");
}
}
break;
} else {
System.out.println("你输入的" + station2 + "不存在,请重新输入下车站");
}
}
if (station1Count>station2Count){
stationCount=station1Count-station2Count;
}else if (station2Count>station1Count){
stationCount=station2Count-station1Count;
}else {
System.out.println("上啥子车,多半脑子不好使");
}
return stationCount;
}
}
02-02
1002
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-07
1243
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-16
345
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)