从hashmap

时间:2017-12-30 23:47:39

标签: java hashmap

我是地图的新手,我正在尝试使用地图制作冒险游戏,并且在尝试检索我在游戏中的某个位置时遇到问题。我试图获取一个整数的映射的值并将其存储在一个整数中,但是当我尝试这样做时,它表示它找到了一个对象而不是一个整数。

package com.example.AdventureGame;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        // write your code here

        Scanner input = new Scanner(System.in);

        Location home = new Location("Home", "Welcome home", 1);
        Location road = new Location("Road", "You are standing in a road", 2);
        Location valley = new Location("Valley", "You are standing at the bottom of a valley, just staring up at it", 3);
        Location hill = new Location("Hill", "You are in the middle of a hill, lost with nowhere to go", 4);
        Location forest = new Location("Forest", "You are lost in a forest, with only a compass", 5);

        Map<Integer, Location> currentLocations = new HashMap<>();

        currentLocations.put(home.getLocationID(), home);
        currentLocations.put(road.getLocationID(), road);
        currentLocations.put(valley.getLocationID(), valley);
        currentLocations.put(hill.getLocationID(), hill);
        currentLocations.put(forest.getLocationID(), forest);

        Map<String, Integer> directionsForHome = new HashMap<>();
        directionsForHome.put("W", 5);

        Map<String, Integer> directionsForRoad = new HashMap<>();
        directionsForRoad.put("N", 1);
        directionsForRoad.put("S", 3);
        directionsForRoad.put("E", 4);


        Map<String, Integer> directionsForValley = new HashMap<>();
        directionsForValley.put("N", 2);
        directionsForValley.put("W", 5);

        Map<String, Integer> directionsForHill = new HashMap<>();
        directionsForHill.put("W", 2);

        Map<String, Integer> directionsForForest = new HashMap<>();
        directionsForForest.put("N", 1);

        List<Map> directions = new ArrayList<>();
        directions.add(directionsForHome);
        directions.add(directionsForRoad);
        directions.add(directionsForValley);
        directions.add(directionsForHill);
        directions.add(directionsForForest);

        Integer loc = 2;
        String directionChosen = "";
        while (true) {
            if (loc == 1) {
                break;
            }
            System.out.println(currentLocations.get(loc).getDescription());
            System.out.println("Current locations are: " + directions.get(loc - 1).keySet());
            directionChosen = input.nextLine().toUpperCase();
            //            loc = directions.get
            if (directions.get(loc - 1).containsKey(directionChosen)) {
                loc = directions.get(loc - 1).get(directionChosen);
            } else {
                System.out.println("Invalid DIRECTION");
            }

            break;
        }
    }
}

以下是Location类的代码:

package com.example.AdventureGame;

import java.util.HashMap;
import java.util.Map;

public class Location {
    private String locationName;
    private String description;
    private int locationID;
    private Map<String, Integer> locations;

    public Location(String locationName, String description, int locationID) {
        this.locationName = locationName;
        this.description = description;
        this.locationID = locationID;
        this.locations = new HashMap<>();
        this.locations.put("Q", 0);
    }

    public String getLocationName() {
        return locationName;
    }

    public String getDescription() {
        return description;
    }

    public int getLocationID() {
        return locationID;
    }
}

错误发生在Main类的行中:

loc = directions.get(loc -1).get(directionChosen);

我非常感谢任何帮助:D谢谢!

0 个答案:

没有答案