(今天也是前幾天的 今天補(bǔ)上)
package com.dzqc.Day0107;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
*
* @Author: zhangxiaokun
* @Date: 2022/01/07/10:49
* @Description:Java基礎(chǔ)-集合:綜合
鄭州一號(hào)線站編號(hào)和站名對應(yīng)關(guān)系如下:
1=河南工業(yè)大學(xué)站
2=鄭大科技園站
3=鄭州大學(xué)站
4=梧桐街站
5=蘭寨站
6=...
將以上對應(yīng)關(guān)系的數(shù)據(jù)存儲(chǔ)到map集合中,key:表示站編號(hào),value:表示站名,并遍歷打印(可以不按順序打印):
第10站: 秦嶺路站
第6站: 鐵爐站
第12站: 碧沙崗站
第13站: 綠城廣場站
2.計(jì)算地鐵票價(jià)規(guī)則:
總行程 :3站以內(nèi),共收 2 元
3站 到 10站 , 每站加收0.5元
10站以上,每兩站加收0.5元,
封頂10元。
3.打印格式(需要對鍵盤錄入的上車站和到達(dá)站進(jìn)行判斷,如果沒有該站,提示重新輸入,直到站名存在為止):
注意:每站需要2分鐘
請輸入上車站:
西二旗
您輸入的上車站:西二旗不存在,請重新輸入上車站:
體育西路
您輸入的上車站:體育西路不存在,請重新輸入上車站:
鄭州火車站
請輸入到達(dá)站:
西二旗
您輸入的到達(dá)站:西二旗不存在,請重新輸入到達(dá)站:
體育西路
您輸入的到達(dá)站:體育西路不存在,請重新輸入到達(dá)站:
鄭州東站
從鄭州火車站到鄭州東站共經(jīng)過10站收費(fèi)5.5元,大約需要 20分鐘
*/
public class Zuoye {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<>();
hm.put(1, "河南工業(yè)大學(xué)");
hm.put(2, "鄭大科技園站");
hm.put(3, "鄭州大學(xué)站");
hm.put(4, "梧桐街站");
hm.put(5, "蘭寨站");
hm.put(6, "鐵爐站");
hm.put(7, "鄭州火車站");
hm.put(8, "第八站");
hm.put(9, "第九站");
hm.put(10, "秦嶺路站");
hm.put(11, "第十一站");
hm.put(12, "碧沙崗站");
hm.put(13, "綠城廣場站");
hm.put(14, "第十四站");
hm.put(15, "第十五站");
hm.put(16, "第十六站");
hm.put(17, "鄭州東站");
//遍歷車站
Set<Integer> sOut = hm.keySet();
for (Integer i : sOut) {
System.out.println(i + "--" + hm.get(i));
}
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String sStart;
String sEnd;
// 獲取上車站
while (true) {
System.out.println("請輸入上車站:");
sStart = sc.nextLine();
if (hm.containsValue(sStart)) {
break;
} else {
System.out.println("站點(diǎn)不存在,請重新輸入");
}
}
// 獲取下車站
while (true) {
System.out.println("請輸入到達(dá)站");
sEnd = sc.nextLine();
if (hm.containsValue(sEnd)) {
break;
} else {
System.out.println("站點(diǎn)不存在,請重新輸入");
}
}
// 獲取站點(diǎn)編號(hào)及間隔差
int iStart = 0;
int iEnd = 0;
int iSub;
Set<Integer> sKey = hm.keySet();
for (Integer i : sKey) {
if (sStart.equals(hm.get(i))) {
iStart = i;
}
if (sEnd.equals(hm.get(i))) {
iEnd = i;
}
}
if (iStart > iEnd) {
iSub = iStart - iEnd;
} else {
iSub = iEnd - iStart;
}
// 計(jì)算金額
int price;
if (iSub <= 3) {
price = 2;
} else if (iSub <= 10) {
price = (int) (iSub * 0.5);
} else {
price = (int) (iSub - 5 * 0.5);
}
// 10元封頂
price = price > 10 ? 10 : price;
// 計(jì)算時(shí)間
int time = iSub * 2;
System.out.println("從[" + sStart + "]到[" + sEnd + "]共經(jīng)過" + iSub + "站,收費(fèi)" + price + "元,大約需要" + time + "分鐘");
}
}