java中Hashmap的get方法是什么?

自己做了一个模拟21点的游戏,做到一半却碰到了点问题。
public class 功能类
{
static int 牌库数量=52;
static int 计分=0;
static int 玩家点数=0;
static int 玩家暗牌点数=0;
static int 玩家记牌=0;
static int 电脑点数=0;
static int 电脑记牌=0;
static int 电脑暗牌点数=0;
static HashMap 卡牌=new HashMap();
public static void 牌数据()
{
卡牌.put(2,"2");
卡牌.put(3,"3");
卡牌.put(4,"4");
卡牌.put(5,"5");
卡牌.put(6,"6");
卡牌.put(7,"7");
卡牌.put(8,"8");
卡牌.put(9,"9");
卡牌.put(10,"10");
卡牌.put(10,"K");
卡牌.put(10,"Q");
卡牌.put(10,"K");
卡牌.put(11,"A");
}
public static void 玩家抓牌()
{ 牌库数量-=1;
玩家记牌+=1;
int i=(int) (Math.random()*9+2);
if (玩家记牌==1){玩家暗牌点数=i;}
玩家点数+=i;

String j=(String)卡牌.get(i);
System.out.println("玩家获得第"+玩家记牌+"张牌:"+j);
System.out.println("玩家目前点数"+玩家点数);
System.out.println("牌库剩余数量"+牌库数量);
if (玩家点数>21) {System.out.println("玩家瞬间爆炸");}
if (玩家点数==21 && 玩家记牌==2)
{
System.out.println("玩家完成了一次black jack!" );
计分+=2;
}
}
==============================================================================
public class 主方法类
{
static boolean 玩家回合=true;
public static void main(String[]args)
{
功能类.玩家抓牌();
}
}
===============================================
为什么取出的value值是null?
我做了一个测试,把他们放到一个类里,就正常了,真是想不明白,求各路大神解救!

map中存储的是键值对,也就是说通过set方法进行参数和值的存储,之后通过get“键”的形式进行值的读取。举例:
Map map = new Hashmap();//创建一个map
map.put("key","value");//给map赋值
String vlaues = map.get("key");//获取map中键值为“key”的值
system.out.println(vlaues );//输出结果
以上代码的运行结果:value;
备注:map中可以存放是字符串,也可以存放的是对象,这个根据实际需要进行调整即可,也可以通过”<T>“的形式来进行对象转换,这个在特定场景下进行特定转换即可。
温馨提示:答案为网友推荐,仅供参考
相似回答