You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中HashMap对象键引用问题:无法获取Player库存Coins

问题分析

你遇到的「coins无法解析为变量」错误,核心原因是:构造方法里定义的coins是局部变量,仅在构造方法的作用域内有效,buyItemPierre方法无法直接访问这个变量。

解决方案

下面提供两种可行的修复方式,根据你的项目需求选择:

方案1:将Coins设为Player类的成员变量

把Coins对象提升为Player类的实例变量,让整个类的方法都能访问它:

private String name;
private HashMap<Item, Integer> inventory;
private String location;
private Resource coins; // 添加成员变量

public Player(String name) {
    this.name = name;
    location = "Home";
    inventory = new HashMap<>();

    this.coins = new Resource("Coins", 1, 1); // 初始化成员变量
    Tool pickaxe = new Tool("Pickaxe", 100, 100);
    Tool axe = new Tool("Axe", 100, 100);
    Tool shovel = new Tool("Shovel", 100, 100);
    Crop turnip = new Crop("Turnip", 20, "Spring");

    inventory.put(this.coins, 50);
    inventory.put(pickaxe, 1);
    inventory.put(axe, 1);
    inventory.put(shovel, 1);
    inventory.put(turnip, 10);
}

public void buyItemPierre(Shop pierres, Item item) {
    if (location.equals("Pierres")) {
        if (pierres.getForSale().containsKey(item)) {
            // 直接使用成员变量访问Coins
            Integer playerCoins = inventory.get(this.coins);
            // 处理coins可能不存在的情况(避免空指针)
            if (playerCoins != null && playerCoins >= pierres.getForSale().get(item)) {
                // 执行购买逻辑:扣除金币、添加物品到库存
                inventory.put(this.coins, playerCoins - pierres.getForSale().get(item));
                inventory.put(item, inventory.getOrDefault(item, 0) + 1);
                System.out.println("Successfully bought " + item.getName());
            } else {
                System.out.println("Not enough coins!");
            }
        } else {
            System.out.println("Sorry, that item is not for sale here");
        }
    } else {
        System.out.println("You have to visit Pierres before you can buy anything from there!");
    }
}

方案2:通过物品名称查找Coins(无需修改类结构)

如果不想新增成员变量,可以遍历inventory的键,找到名称为"Coins"的物品(前提是Item类有getName()方法):

public void buyItemPierre(Shop pierres, Item item) {
    if (location.equals("Pierres")) {
        if (pierres.getForSale().containsKey(item)) {
            // 查找inventory中的Coins
            Item coinsKey = null;
            for (Item invItem : inventory.keySet()) {
                if ("Coins".equals(invItem.getName())) {
                    coinsKey = invItem;
                    break;
                }
            }

            if (coinsKey == null) {
                System.out.println("You have no coins!");
                return;
            }

            Integer playerCoins = inventory.get(coinsKey);
            if (playerCoins != null && playerCoins >= pierres.getForSale().get(item)) {
                // 执行购买逻辑
                inventory.put(coinsKey, playerCoins - pierres.getForSale().get(item));
                inventory.put(item, inventory.getOrDefault(item, 0) + 1);
                System.out.println("Successfully bought " + item.getName());
            } else {
                System.out.println("Not enough coins!");
            }
        } else {
            System.out.println("Sorry, that item is not for sale here");
        }
    } else {
        System.out.println("You have to visit Pierres before you can buy anything from there!");
    }
}
注意事项
  • 一定要处理inventory.get()返回null的情况,避免NullPointerException(比如玩家的coins被意外移除的极端场景)。
  • 如果使用方案2,确保Item类有getName()方法,否则无法通过名称匹配查找。

内容的提问来源于stack exchange,提问作者Sofie Buur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 04:45:40