Java_CH11_HW2

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Java_CH11_HW2
  5. {
  6.  
  7. public static void main(String[] args) throws IOException
  8. {
  9. // TODO 自動產生的方法 Stub
  10. TreeMap<Integer, String> map = new TreeMap<>();
  11. String filepath = "C:\\Users\\Dragonyue\\Desktop\\第 11 章 作業參考文件\\Freeway1.txt";
  12. BufferedReader reader = null;
  13. Scanner scanner = new Scanner(System.in);
  14. String input;
  15. int start, end;
  16. try
  17. {
  18. System.out.print("輸入 起點 終點:");
  19. start = (int) Math.floor(scanner.nextDouble());
  20. end = (int) Math.ceil(scanner.nextDouble());
  21. reader = new BufferedReader(new FileReader(filepath));
  22. while ((input = reader.readLine()) != null)
  23. {
  24. String[] temp = input.split("\t");
  25. map.put(Integer.parseInt(temp[1]), temp[0]);
  26. }
  27. if(start> end)
  28. {
  29. int temp = start;
  30. start = end;
  31. end = temp;
  32. }
  33. if (map.get(start) == null)
  34. start = map.higherKey(start);
  35. if (map.get(end) == null)
  36. end = map.lowerKey(end);
  37. System.out.println(String.format("交流道 :%s 至 %s 從路標 %d 至 %d 距離 = %d",
  38. map.get(start), map.get(end), start, end, end - start));
  39. }
  40. catch (IOException e)
  41. {
  42. // TODO 自動產生的 catch 區塊
  43. e.printStackTrace();
  44. }
  45. finally
  46. {
  47. if (reader != null)
  48. reader.close();
  49. }
  50.  
  51. }
  52.  
  53. }
  54.  

留言

這個網誌中的熱門文章

C# 井字遊戲