【发布时间】:2011-06-28 10:27:08
【问题描述】:
在下面的代码中,我试图计算两个城市之间的距离。用户输入城市名称和城市,然后用户输入这些城市之间的距离,最后输入这些城市之间的旅行价格。我还没有对其进行评估,因为我不确定我将如何做到这一点。我的问题是我正在寻找有关如何做到这一点的指针和建议。
import java.io.*;
import java.util.*;
public class CityCalcultor {
static LinkedList<String> cities = new LinkedList<String>();
static LinkedList<Integer> distance = new LinkedList<Integer>();
static LinkedList<Integer> price = new LinkedList<Integer>();
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
String text;
int option = 0;
while (true) {
System.out.println("\nWhat would you like to do:\n"
+ "1. Add a city to the system\n"
+ "2. Add a path to the system\n"
+ "3. Evalute paths\n"
+ "4. Exit\n" + "Your option: ");
text = input.nextLine();
option = Integer.parseInt(text);
switch (option) {
case 1:
EnterCity();
break;
case 2:
EnterPath();
break;
case 3:
EvalutePaths();
break;
case 4:
return;
default:
System.out.println("ERROR INVALID INPUT");
}
}
}
public static void EnterCity() {
String c = "";
LinkedList<String> cities = new LinkedList<String>(Arrays.asList(c));
Scanner City = new Scanner(System.in);
System.out.println("Please enter the city name ");
c = City.nextLine();
cities.add(c);
System.out.println("City " + c + " has been added ");
}
public static void EnterPath() {
Scanner Path = new Scanner(System.in);
int d = 0;
int p = 0;
System.out.println("Enter the starting city ");
System.out.println();
System.out.println(Path.nextLine());
System.out.println("Enter the ending city ");
System.out.println(Path.nextLine());
System.out.println("Enter the distance between the two cities ");
d = Path.nextInt();
for (d = 0; d > 0; d++) {
distance.add(d);
}
System.out.println("Enter the price between the two cities ");
p = Path.nextInt();
for (p = 0; p > 0; p++) {
price.add(p);
}
System.out.println("The route was sucessfully added ");
}
private static void EvalutePaths() {
}
}
【问题讨论】:
-
这引出了一个问题:您是否正在为旅行推销员寻找解决方案(正如@trashgod 建议的那样)?这个问题就是要找到最短的游览,它只访问每个城市一次。或者您是否正在寻找任何两个城市之间的最短路线(一个更容易的问题)?请验证evaluate一词的含义。
-
不...这只是为了查找任何两个城市之间的最短路线。
标签: java shortest-path