在Java中使用HashMaps

在Java中使用HashMaps,java,Java,基本上,我是用BlueJ制作这个Java程序的,在这个程序中,玩家是在魔戒的世界里。我为武器、物品等创建了单独的包。我在所有包之外有一个类Main(在项目屏幕的主体中)。在那里,我尝试了一些东西 public static void test()throws Exception{ System.out.println("There is a brass sword and an iron sword. Which do you want?"); Scanner i

基本上,我是用BlueJ制作这个Java程序的,在这个程序中,玩家是在魔戒的世界里。我为武器、物品等创建了单独的包。我在所有包之外有一个类Main(在项目屏幕的主体中)。在那里,我尝试了一些东西

public static void test()throws Exception{
        System.out.println("There is a brass sword and an iron sword. Which do you want?");
        Scanner in = new Scanner(System.in);
        String s = in.next();
        HashMap options = new HashMap();
        options.put("brass", new Sword());
        options.put("iron", new Sword());
        Sword k = options.get(s);
}

我想用上面的方法把剑的东西还给我。不幸的是,这不起作用。有什么帮助吗?

只需使用参数化类型
HashMap
,将
HashMap
声明为

HashMap<String, Sword> options = new HashMap<String, Sword>();
HashMap options=newhashmap();
我想用上面的方法把剑的东西还给我

然后更改方法返回类型并向其添加返回:

public static Sword test()throws Exception{
        System.out.println("There is a brass sword and an iron sword. Which do you want?");
        Scanner in = new Scanner(System.in);
        String s = in.next();
        HashMap<String, Sword> options = new HashMap<String, Sword>();
        options.put("brass", new Sword());
        options.put("iron", new Sword());
        Sword k = options.get(s);
        return k;
}
公共静态剑测试()引发异常{
有一把铜剑和一把铁剑,你想要哪把;
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();
HashMap选项=新建HashMap();
期权。卖出(“黄铜”,新剑());
期权。看跌期权(“铁”,新剑());
剑k=选项。获取(s);
返回k;
}

只需使用参数化类型
HashMap
,将
HashMap
声明为

HashMap<String, Sword> options = new HashMap<String, Sword>();
HashMap options=newhashmap();
我想用上面的方法把剑的东西还给我

然后更改方法返回类型并向其添加返回:

public static Sword test()throws Exception{
        System.out.println("There is a brass sword and an iron sword. Which do you want?");
        Scanner in = new Scanner(System.in);
        String s = in.next();
        HashMap<String, Sword> options = new HashMap<String, Sword>();
        options.put("brass", new Sword());
        options.put("iron", new Sword());
        Sword k = options.get(s);
        return k;
}
公共静态剑测试()引发异常{
有一把铜剑和一把铁剑,你想要哪把;
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();
HashMap选项=新建HashMap();
期权。卖出(“黄铜”,新剑());
期权。看跌期权(“铁”,新剑());
剑k=选项。获取(s);
返回k;
}

只需使用参数化类型
HashMap
,将
HashMap
声明为

HashMap<String, Sword> options = new HashMap<String, Sword>();
HashMap options=newhashmap();
我想用上面的方法把剑的东西还给我

然后更改方法返回类型并向其添加返回:

public static Sword test()throws Exception{
        System.out.println("There is a brass sword and an iron sword. Which do you want?");
        Scanner in = new Scanner(System.in);
        String s = in.next();
        HashMap<String, Sword> options = new HashMap<String, Sword>();
        options.put("brass", new Sword());
        options.put("iron", new Sword());
        Sword k = options.get(s);
        return k;
}
公共静态剑测试()引发异常{
有一把铜剑和一把铁剑,你想要哪把;
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();
HashMap选项=新建HashMap();
期权。卖出(“黄铜”,新剑());
期权。看跌期权(“铁”,新剑());
剑k=选项。获取(s);
返回k;
}

只需使用参数化类型
HashMap
,将
HashMap
声明为

HashMap<String, Sword> options = new HashMap<String, Sword>();
HashMap options=newhashmap();
我想用上面的方法把剑的东西还给我

然后更改方法返回类型并向其添加返回:

public static Sword test()throws Exception{
        System.out.println("There is a brass sword and an iron sword. Which do you want?");
        Scanner in = new Scanner(System.in);
        String s = in.next();
        HashMap<String, Sword> options = new HashMap<String, Sword>();
        options.put("brass", new Sword());
        options.put("iron", new Sword());
        Sword k = options.get(s);
        return k;
}
公共静态剑测试()引发异常{
有一把铜剑和一把铁剑,你想要哪把;
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();
HashMap选项=新建HashMap();
期权。卖出(“黄铜”,新剑());
期权。看跌期权(“铁”,新剑());
剑k=选项。获取(s);
返回k;
}

如果希望方法返回剑对象,则应将
公共静态无效测试()
更改为
公共静态剑测试()
,并在方法调用的末尾
返回剑对象
如果希望方法返回剑对象,则应更改
公共静态无效测试()
公共静态剑测试()
并在方法调用的末尾
返回剑

如果希望方法返回剑对象,则应将
公共静态无效测试()
更改为
公共静态剑测试()
在方法调用的末尾
返回剑

如果希望方法返回剑对象,则应将
公共静态无效测试()
更改为
公共静态剑测试()
,并在方法调用的末尾
返回剑
使用以下代码:

public static Sword test()throws Exception{
    System.out.println("There is a brass sword and an iron sword. Which do you want?");
    Scanner in = new Scanner(System.in);
    String s = in.next();
    HashMap<String, Sword> options = new HashMap<String, Sword>();
    options.put("brass", new Sword());
    options.put("iron", new Sword());
    return options.get(s);
}
公共静态剑测试()引发异常{
有一把铜剑和一把铁剑,你想要哪把;
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();
HashMap选项=新建HashMap();
期权。卖出(“黄铜”,新剑());
期权。看跌期权(“铁”,新剑());
返回选项。获取;
}
使用以下代码:

public static Sword test()throws Exception{
    System.out.println("There is a brass sword and an iron sword. Which do you want?");
    Scanner in = new Scanner(System.in);
    String s = in.next();
    HashMap<String, Sword> options = new HashMap<String, Sword>();
    options.put("brass", new Sword());
    options.put("iron", new Sword());
    return options.get(s);
}
公共静态剑测试()引发异常{
有一把铜剑和一把铁剑,你想要哪把;
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();
HashMap选项=新建HashMap();
期权。卖出(“黄铜”,新剑());
期权。看跌期权(“铁”,新剑());
返回选项。获取;
}
使用以下代码:

public static Sword test()throws Exception{
    System.out.println("There is a brass sword and an iron sword. Which do you want?");
    Scanner in = new Scanner(System.in);
    String s = in.next();
    HashMap<String, Sword> options = new HashMap<String, Sword>();
    options.put("brass", new Sword());
    options.put("iron", new Sword());
    return options.get(s);
}
公共静态剑测试()引发异常{
有一把铜剑和一把铁剑,你想要哪把;
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();
HashMap选项=新建HashMap();
期权。卖出(“黄铜”,新剑());
期权。看跌期权(“铁”,新剑());
返回选项。获取;
}
使用以下代码:

public static Sword test()throws Exception{
    System.out.println("There is a brass sword and an iron sword. Which do you want?");
    Scanner in = new Scanner(System.in);
    String s = in.next();
    HashMap<String, Sword> options = new HashMap<String, Sword>();
    options.put("brass", new Sword());
    options.put("iron", new Sword());
    return options.get(s);
}
公共静态剑测试()引发异常{
有一把铜剑和一把铁剑,你想要哪把;
扫描仪输入=新扫描仪(系统输入);
字符串s=in.next();
HashMap选项=新建HashMap();
期权。卖出(“黄铜”,新剑());
期权。看跌期权(“铁”,新剑());
返回选项。获取;
}

默认hashMap接受两种泛型类型
hashMap
,它们代表
hashMap
,您必须在代码中使用
选项。将
转换为
,但这样您就不会使用泛型的功能,因此建议使用@BackSlash的答案,因为您不需要转换


有关泛型的更多信息:

默认hashMap接受两种泛型类型
hashMap
,它们代表
hashMap
,您可以在代码中使用它们