我是Java语言的新手,我有一个项目要做。我需要一些帮助来完成我的任务,这并不难,但我真的不明白。我拥有这个XML文件
<LOCATION_PROPERTIES NAME="STN10">
<LOCATION ID="10" TYPE="P+D" X="52380" Y="24020" ANGLE="90" PICK_BANK="2" PICK_TABLE="10" PICK_MOD="0" PICK_DRV="N->P" DROP_BANK="2" DROP_TABLE="11" DROP_MOD="0" DROP_DRV="N->P"/>
<SEGMENT_PROPERTIES>
<NEXT_LOCATION NAME="X535" BANK="3" TABLE="127" MOD="0" DRV="P->P" INITIAL_SPEED="600" SPEED="504" LENGTH="8052" STOP="CS"/>
</SEGMENT_PROPERTIES>
</LOCATION_PROPERTIES>
<LOCATION_PROPERTIES NAME="STN11">
<LOCATION ID="11" TYPE="P+D" X="53280" Y="24020" ANGLE="90" PICK_BANK="2" PICK_TABLE="10" PICK_MOD="0" PICK_DRV="N->P" DROP_BANK="2" DROP_TABLE="11" DROP_MOD="0" DROP_DRV="N->P"/>
<SEGMENT_PROPERTIES>
<NEXT_LOCATION NAME="X535" BANK="3" TABLE="58" MOD="0" DRV="P->P" INITIAL_SPEED="600" SPEED="502" LENGTH="7893" STOP="CS"/>
</SEGMENT_PROPERTIES>
</LOCATION_PROPERTIES>
<LOCATION_PROPERTIES NAME="STN12">
<LOCATION ID="12" TYPE="P+D" X="54180" Y="24020" ANGLE="90" PICK_BANK="2" PICK_TABLE="10" PICK_MOD="0" PICK_DRV="N->P" DROP_BANK="2" DROP_TABLE="11" DROP_MOD="0" DROP_DRV="N->P"/>
<SEGMENT_PROPERTIES>
<NEXT_LOCATION NAME="X534" BANK="3" TABLE="59" MOD="0" DRV="P->P" INITIAL_SPEED="600" SPEED="600" LENGTH="4267" STOP="CS"/>
</SEGMENT_PROPERTIES>
</LOCATION_PROPERTIES>
<LOCATION_PROPERTIES NAME="X535">
<LOCATION ID="535" TYPE="MOVE" X="54090" Y="33800" ANGLE="90"/>
<SEGMENT_PROPERTIES>
<NEXT_LOCATION NAME="X536" BANK="1" TABLE="88" MOD="0" DRV="P->P" INITIAL_SPEED="1200" SPEED="1200" LENGTH="3013" STOP="CS"/>
<NEXT_LOCATION NAME="STN19" BANK="2" TABLE="65" MOD="0" DRV="P->N" INITIAL_SPEED="1043" SPEED="468" LENGTH="5438" STOP="CS"/>
<NEXT_LOCATION NAME="STN20" BANK="2" TABLE="66" MOD="0" DRV="P->N" INITIAL_SPEED="1200" SPEED="513" LENGTH="6338" STOP="CS"/>
<NEXT_LOCATION NAME="STN22" BANK="2" TABLE="198" MOD="0" DRV="P->N" INITIAL_SPEED="1200" SPEED="587" LENGTH="8138" STOP="CS"/>
<NEXT_LOCATION NAME="STN21" BANK="2" TABLE="67" MOD="0" DRV="P->N" INITIAL_SPEED="1200" SPEED="552" LENGTH="7238" STOP="CS"/>
</SEGMENT_PROPERTIES>
</LOCATION_PROPERTIES>
我成功地向所有下一个地点展示了所有车站和时间。它给了我:
出发站:STN10。 抵达:X535 ---时间:16秒
出发站:STN11 抵达:X535 ---时间:16秒
... //等等,很多
出发站:X535 抵达:X536 ---时间:2秒 抵达:STN19 ---时间:12秒 抵达:STN20 ---到达时间:12秒 ...... //等
我需要做的是显示从一个站到另一个站的时间。有点像这样:
出发站:STN10 抵达:X535 ---时间:16秒 抵达:X536 ---时间:2秒 到货:X537 --- ...... 到货:STN26 ---总时间:...... ...... //等
但是现在我有点陷入困境,我认为我必须以递归方式进行,但我不知道如何继续。
目前,这是我的java代码,但我需要创建更多我认为的函数。
public static int onestep4all()
{
//int cpt = 0;
int timeonestep = 0;
List <Element> location_properties = root.getChildren("LOCATION_PROPERTIES");
for (Element loc : location_properties)
{
if(loc.getAttributeValue("NAME").startsWith("STN")== true)
{
System.out.println("-------------------------------------------------");
System.out.println(" Departure Station : "+loc.getAttributeValue("NAME"));
List <Element> segment_properties = loc.getChildren("SEGMENT_PROPERTIES");
for (Element seg : segment_properties)
{
List <Element> next_location = seg.getChildren("NEXT_LOCATION");
for (Element next: next_location)
{
System.out.print("Arrival : " +next.getAttributeValue("NAME"));
int L = Integer.parseInt(next.getAttributeValue("LENGTH"));
int S = Integer.parseInt(next.getAttributeValue("SPEED"));
timeonestep = L/S;
System.out.println(" --- Time to go : "+timeonestep+" seconds");
}
}
}
}
return timeonestep;
}
谢谢大家的帮助。