我试图在地图上的两点之间找到一条路径。
当它从循环中跳出并返回权重时,它会转到 else 语句并再次调用 find。为什么代码会这样做?
public int find() throws LinkException {
Node currentNode = map.getNode(origin);
int weight = 0;
return find(currentNode, null, weight);
}
private int find(Node currentNode, Node pastNode, int weight) throws LinkException {
for (Node futureNode : currentNode.getLinks()) {
if (currentNode == futureNode || futureNode == pastNode) {
continue;
}
weight += currentNode.getLink(futureNode).getWeight();
pastNode = currentNode;
currentNode = futureNode;
if (currentNode.getName().equals(destination)) { // Here we reach the destination
break;
} else {
find(currentNode, pastNode, weight);
}
}
return weight;
}