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