【发布时间】:2017-01-24 15:47:23
【问题描述】:
我正在构建一个货运管理系统。在用户输入源端口和目标端口后,我想列出源和目标之间可用的中间端口。但我不知道该怎么做。我将每个端口的端口名称和距离保存在文本文件中,其中每个端口一行。位置变量用于计算源和目的地之间的距离。我想使用位置变量来定位源和目标之间的端口。
新闻;10;
哈里敦;25;
真的;33;
东港;47;
雅典;56;
纳斯基;71;
private int location, location1, position, position1;
private String source, destination;
public void choosePort(){
Scanner sc = new Scanner(System.in);
int i = 0;
System.out.println("Please enter the source.");
source = sc.nextLine();
System.out.println("Please enter the destination.");
destination = sc.nextLine();
try{
File f = new File("Port.txt");
Scanner file = new Scanner(f);
while(file.hasNextLine()){
String line = file.nextLine();
String[] split = line.split(";");
if(split[0].equals(source)){
location = Integer.parseInt(split[1]);
position = i;
}
else if(split[0].equals(destination)){
location1 = Integer.parseInt(split[1]);
position1 = i;
}
i++;
}
file.close();
}
catch(IOException e){
System.out.println(e);
}
【问题讨论】:
-
请将源代码作为文本粘贴到您的问题中,而不是图像。还有什么不适用于您当前的代码?
-
所有端口都在同一条几何线上吗?
-
不,它们在不同的行中。