1
我在Location類中實現了Iterable
,它存儲了一個對象映射圖,以允許迭代地圖中的每個對象。如何使用.next()迭代對象地圖?
但是,當我測試這個代碼時,只有第一個位置加載,即「Tiberius」 當我給輸入一個,move
命令。
有沒有人爲什麼地圖中的下一個位置未在此實現中加載?
這是一個位置添加到地圖和迭代方法:
private Map<Location, Integer> children = new HashMap<Location, Integer>();
@Override
public Iterator<Location> iterator() {
return children.keySet().iterator();
}
主類,其中的.next()是在起始位置稱爲:
public class Main {
public static void main(String[] args) {
//Boolean to signify game state
boolean gameNotOver = true;
Location nextLocation = new Location();
Location startNode = new Location();
Iterator<Location> nodeIterator = startNode.iterator();
GameMap playerMap = new GameMap();
playerMap.getStartNode();
//get the first location in the map
startNode = playerMap.getStartNode();
//main game loop
while (gameNotOver) {
Main mainObj = new Main();
//Take in user commands here
//and parse commands
String input = Input.getInput();
if (input.equals("description")) {
System.out.println("Description: ");
} else if (input.equals("move")) {
System.out.println("Moving.. ");
//call the next location in the map
startNode.next();
System.out.println(startNode.toString());
//Invalid input check
else {
System.out.println("Invalid command, try again!");
}
}
//Game over
System.out.println("Game Over!");
System.exit(0);
}
}
您正在設置'nodeIterator()',但沒有使用它,並且您在'Location'上調用'next()',這是你沒有向我們顯示的。 – RealSkeptic
你在哪裏插入項目到'playerMap'中?請提供可編譯的代碼。 – Oswald