Java_CH11_HW2

import java.io.*;
import java.util.*;

public class Java_CH11_HW2
{

 public static void main(String[] args) throws IOException
 {
  // TODO 自動產生的方法 Stub
  TreeMap<Integer, String> map = new TreeMap<>();
  String filepath = "C:\\Users\\Dragonyue\\Desktop\\第 11 章 作業參考文件\\Freeway1.txt";
  BufferedReader reader = null;
  Scanner scanner = new Scanner(System.in);
  String input;
  int start, end;
  try
  {
   System.out.print("輸入 起點 終點:");
   start = (int) Math.floor(scanner.nextDouble());
   end = (int) Math.ceil(scanner.nextDouble());
   reader = new BufferedReader(new FileReader(filepath));
   while ((input = reader.readLine()) != null)
   {
    String[] temp = input.split("\t");
    map.put(Integer.parseInt(temp[1]), temp[0]);
   }
   if(start> end)
   {
    int temp = start;
    start = end;
    end = temp;
   }
   if (map.get(start) == null)
    start = map.higherKey(start);
   if (map.get(end) == null)
    end = map.lowerKey(end);
   System.out.println(String.format("交流道 :%s 至 %s 從路標 %d 至 %d 距離 = %d",
     map.get(start), map.get(end), start, end, end - start));
  }
  catch (IOException e)
  {
   // TODO 自動產生的 catch 區塊
   e.printStackTrace();
  }
  finally
  {
   if (reader != null)
    reader.close();
  }

 }

}

留言

這個網誌中的熱門文章

UVA 11321 Java