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 :(得分:0)
我真的不明白你的输入 - 从你的电台阵列来看,它看起来像是一条直线。但是,要做距离算法,你需要使用像Djikstra算法(它计算从一个节点到所有其他节点的最短距离)。有关更多信息和一些伪代码http://en.wikipedia.org/wiki/Djikstra%27s_algorithm