A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

package other;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/*1.项目根路径下有stations.txt文件,内容如下:
朱辛庄
育知路
平西府
回龙观东大街
霍营
育新
西小口
//....
2.利用IO流的知识读取stations.txt文件,将内容写入到subway8.txt,写入格式如下:
1=朱辛庄
2=育知路
3=平西府
4=回龙观东大街
5=霍营
//...
3.读取subway8.txt文件内容存储到map集合中,
key:表示第几站,value:表示站名,并遍历打印(可以不按顺序打印):
    第10站: 森林公园南门
第6站: 育新
第12站: 奥体中心
第13站: 北土城
//...
4.计算地铁票价规则:
总行程 3站内(包含3站)收费3元,
3站以上但不超过5站(包含5站)的收费4元,
5站以上的,在4元的基础上,每多1站增加2元,
10元封顶;
5.打印格式(需要对键盘录入的上车站和到达站进行判断,如果没有该站,提示重新输入,直到站名存在为止):
注意:每站需要2分钟
    请输入上车站:
沙河
您输入的上车站:   沙河 不存在,请重新输入上车站:
上地
您输入的上车站:    上地 不存在,请重新输入上车站:
朱辛庄
请输入到达站:
沙河
您输入的到达站: 沙河 不存在,请重新输入到达站:
西二旗
您输入的到达站: 西二旗 不存在,请重新输入到达站:
西小口
从    朱辛庄    到    西小口    共    经过6    站    收费6元,大约需要           12   分钟 */

public class Test4 {
        static Map<Integer, String> map = new HashMap<Integer, String>();
        static ArrayList<String> array = new ArrayList<String>();

        public static void main(String[] args) throws IOException {
                // num01
                // method1();
                method2();

                /*
                 */
                /*
                 * key是固定的,利用key做參照物
                 */
                // 此方法獲取坐了多少站
                // boolean flag3 = map.containsValue("霍营");
                // System.out.println(flag3);

                 int stations = method4();
                 System.out.println(stations);
                // getMoney(int num)獲取要花多少錢
                // getMoney(stations);

        }

        public static void getMoney(int num) {
                /**
                 * 总行程 3站内(包含3站)收费3元, 3站以上但不超过5站(包含5站)的收费4元, 5站以上的,在4元的基础上,每多1站增加2元,
                 * 10元封顶;
                 */
                if (num <= 3) {
                        System.out.println("收費3元");
                }
                if (num > 3 && num < 5) {
                        System.out.println("收費4元");
                }
                if (num > 5) {
                        int money = num - 5 + 4;
                        if (money >= 10) {
                                System.out.println("收費10元");
                        } else {
                                System.out.println("收費:" + money + "元");
                        }
                }
        }

        public static int method4() {
                Scanner sc = new Scanner(System.in);
                boolean flag = true;
                boolean flag2 = true;
                int key1 = -1;

                int key2 = -1;
                // 判斷起點站
                System.out.println("請輸入起點站:");
                String start = sc.nextLine();
                while (flag){
                         
                        if (map.containsKey(start)) {
                                Set<Map.Entry<Integer, String>> entrys = map.entrySet();
                                for (Map.Entry<Integer, String> entry : entrys) {
                                        int key = entry.getKey();
                                        String value = entry.getValue();
                                        if (value.equals(start)) {
                                                key1 = key;
                                                flag = false;
                                        }
                                }
                        }else {
                                System.out.println("沒有這個站,請重新輸入:");
                                start = sc.nextLine();
                        }
                }

                // 判斷終點站
                 System.out.println("請輸入終點站:");
                 String end = sc.nextLine();
                while (flag2){
                       
                        if (map.containsKey(end)) {
                                Set<Map.Entry<Integer, String>> entrys = map.entrySet();
                                for (Map.Entry<Integer, String> entry : entrys) {
                                        int key = entry.getKey();
                                        String value = entry.getValue();
                                        if (value.equals(end)) {
                                                key2 = key;
                                                flag2 = false;
                                        }
                                }
                        }else {
                                System.out.println("沒有這個站,請重新輸入:");
                                end = sc.nextLine();
                        }
                }
                return key2 - key1;
        }

        public static void method2() throws FileNotFoundException, IOException {
                /*
                 * 读取subway8.txt文件内容存储到map集合中, key:表示第几站,value:表示站名,并遍历打印(可以不按顺序打印):
                 */
                // 利用集合存儲字符串
                ArrayList<String> array = new ArrayList<String>();

                BufferedReader fr = new BufferedReader(new FileReader("subway8"));
                String s = null;
                while ((s = fr.readLine()) != null) {
                        array.add(s);
                }
                // 遍歷集合,分割字符串,存入Map
                for (String string : array) {
                        String[] str = string.split("=");
                        int key = Integer.parseInt(str[0]);
                        String value = str[1];
                        map.put(key,value);
                }
                // 打印Map
                Set<Map.Entry<Integer, String>> entrys = map.entrySet();
                for (Map.Entry<Integer, String> entry : entrys) {
                        int key = entry.getKey();
                        String value = entry.getValue();
                        array.add(value);
                        System.out.println(key+"="+value);
                }
        }

        public static void method1() throws FileNotFoundException, IOException {
                // subway8.txt
                // 創建輸入輸出劉對象
                BufferedReader fr = new BufferedReader(new FileReader("stations.txt"));
                BufferedWriter fw = new BufferedWriter(new FileWriter("subway8"));
                String s = null;
                int i = 1;
                while ((s = fr.readLine()) != null) {
                        fw.write(i + "=" + s);
                        i++;
                        fw.newLine();
                        fw.flush();
                }
                fw.close();
                fr.close();
        }
}

1 个回复

倒序浏览
貌似字符串不能比较吧,是比较字符串长度吗
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马