2011-01-07 50 views
-1

。我正在編寫一個程序來計算車站之間的距離。我想從用戶的位置以及他們想去的地方獲得輸入。這是我迄今爲止。我被卡住了。接下來我該做什麼?短路徑。距離計算。 java的。請致電

import java.util.Arrays; 

public class StationDist { 
    public static void main(String[] args) { 
     double[] stations = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; 
     //Set up the stations array of doubles here 

     double[][] distances = new double[stations.length][]; 
     for(int i=0; i < stations.length; i++) { 
      distances[i] = new double[i+1]; 
      for(int j=0; j<distances[i].length; j++) { 
       distances[i][j] = Math.abs(stations[i] - stations[j]); 
      } 
      //System.out.println(Arrays.toString(distances[i])); 
     } 
     System.out.println("Enter the destnation: "); 
     System.out.println("1 for london."); 
     System.out.println("2 for bristol"); 
     System.out.println("3 for oxford"); 
     System.out.println("4 for warwick"); 
     KeyboardInput in = new KeyboardInput(); 
     int val = in.readInteger(); 
     System.out.println(Arrays.toString(distances[val])); 
    } 

} 
+0

所以距離是直線?我有點困惑,因爲我想你會想要一個圖結構來使用,例如,Dijkstra的算法,但我不確定你的作業分配的上下文是什麼。 – 2011-01-07 02:13:46

回答

0

我不是很瞭解您的輸入 - 從您的電臺陣列看起來像是一切都在一條直線上。但是,要進行距離算法,您需要使用像Djikstra算法(計算從一個節點到所有其他節點的最短距離)的算法。欲瞭解更多信息和一些僞代碼,http://en.wikipedia.org/wiki/Djikstra%27s_algorithm