在判斷玩家ID中的問(wèn)題、求大神
package?com.joker.test; import?java.util.ArrayList; import?java.util.Iterator; import?java.util.List; import?java.util.Map; import?java.util.Scanner; import?com.joker.entity.Player; import?com.joker.entity.Poker; public?class?PokerGame?{ Map<Integer,?Player>?playerMap; List<Poker>?pokerList; Scanner?input; String[]?type?=?{?"黑桃",?"紅桃",?"梅花",?"方塊"?}; String[]?pointer?=?{?"2",?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"10",?"J", "Q",?"K",?"A"?}; public?PokerGame()?{ pokerList?=?new?ArrayList<Poker>(); input?=?new?Scanner(System.in); } /** ?*?創(chuàng)建撲克牌 ?*? ?*?@param?args ?*/ public?void?pokerAdd()?{ System.out.println("-------創(chuàng)建撲克牌-------"); for?(int?i?=?0;?i?<?type.length;?i++)?{ for?(int?j?=?0;?j?<?pointer.length;?j++)?{ pokerList.add(new?Poker(type[i],?pointer[j])); } } System.out.println("-------撲克牌創(chuàng)建成功-------"); } /** ?*?遍歷顯示所有撲克牌 ?*? ?*?@param?args ?*/ public?void?pokerGet()?{ for?(Iterator<Poker>?it?=?pokerList.iterator();?it.hasNext();)?{ for?(int?i?=?0;?i?<?type.length;?i++)?{ for?(int?j?=?0;?j?<?pointer.length;?j++)?{ Poker?poker?=?it.next(); System.out .print(poker.getType()?+?poker.getPointer()?+?"?"); } System.out.println(); } } } /** ?*?創(chuàng)建玩家 ?*? ?*?@param?args ?*/ public?void?playerAdd()?{ System.out.println("-------創(chuàng)建玩家-------"); int?i?=?0; while?(i?<?2)?{ System.out.println("請(qǐng)輸入玩家的ID:"); int?id; try?{ id?=?input.nextInt(); Player?playerID?=?playerMap.get(id); if?(playerID?==?null)?{ System.out.println("請(qǐng)輸入玩家的姓名:"); String?name?=?input.next(); Player?player?=?new?Player(id,?name); playerMap.put(id,?player); System.out.println("成功創(chuàng)建玩家:"?+playerMap.get(id).getName()); System.out.println("--------------------"); i++; }?else?{ System.out.println("該ID已被占用~~"); continue; } }?catch?(Exception?e)?{ System.out.println("請(qǐng)輸入整數(shù)類型ID!!"); continue; } } } public?static?void?main(String[]?args)?{ PokerGame?pg?=?new?PokerGame(); pg.pokerAdd(); pg.pokerGet(); pg.playerAdd(); } }
為什么我在try-catch語(yǔ)句中會(huì)出現(xiàn)死循環(huán)錯(cuò)誤輸出???求大神指導(dǎo)該怎么改??
2015-09-13
Scanner 對(duì)象最好不要重復(fù)使用,在id=input.nextInt();上面新建一個(gè)Scanner對(duì)象就可以了,要放在try語(yǔ)句塊中,我也遇到了這樣的問(wèn)題,就是這樣解決的
2015-10-16
http://zhidao.baidu.com/link?url=vm_Aygd2FHYeWf5cUqzZKV3fakn7c-HLbk6qGmyTzj1I3x9vHTbTh7wCkQ38fshRTBBLuTmc45coXzXb2TJphJKu1ARC2lh-3dySI4ETf-7
按方法修改后,你的代碼仍有bug
73行拋出java.lang.NullPointerException。
2015-09-08
在創(chuàng)建玩家方法中playerAdd() 的
id=input.nextInt();上面添加一句這個(gè)試試
Scanner input=new Scanner(System.in);