在判斷玩家ID中的問題、求大神
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); } /** ?*?創建撲克牌 ?*? ?*?@param?args ?*/ public?void?pokerAdd()?{ System.out.println("-------創建撲克牌-------"); 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("-------撲克牌創建成功-------"); } /** ?*?遍歷顯示所有撲克牌 ?*? ?*?@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(); } } } /** ?*?創建玩家 ?*? ?*?@param?args ?*/ public?void?playerAdd()?{ System.out.println("-------創建玩家-------"); int?i?=?0; while?(i?<?2)?{ System.out.println("請輸入玩家的ID:"); int?id; try?{ id?=?input.nextInt(); Player?playerID?=?playerMap.get(id); if?(playerID?==?null)?{ System.out.println("請輸入玩家的姓名:"); String?name?=?input.next(); Player?player?=?new?Player(id,?name); playerMap.put(id,?player); System.out.println("成功創建玩家:"?+playerMap.get(id).getName()); System.out.println("--------------------"); i++; }?else?{ System.out.println("該ID已被占用~~"); continue; } }?catch?(Exception?e)?{ System.out.println("請輸入整數類型ID!!"); continue; } } } public?static?void?main(String[]?args)?{ PokerGame?pg?=?new?PokerGame(); pg.pokerAdd(); pg.pokerGet(); pg.playerAdd(); } }
為什么我在try-catch語句中會出現死循環錯誤輸出???求大神指導該怎么改??
2015-09-13
Scanner 對象最好不要重復使用,在id=input.nextInt();上面新建一個Scanner對象就可以了,要放在try語句塊中,我也遇到了這樣的問題,就是這樣解決的
2015-10-16
http://zhidao.baidu.com/link?url=vm_Aygd2FHYeWf5cUqzZKV3fakn7c-HLbk6qGmyTzj1I3x9vHTbTh7wCkQ38fshRTBBLuTmc45coXzXb2TJphJKu1ARC2lh-3dySI4ETf-7
按方法修改后,你的代碼仍有bug
73行拋出java.lang.NullPointerException。
2015-09-08
在創建玩家方法中playerAdd() 的
id=input.nextInt();上面添加一句這個試試
Scanner input=new Scanner(System.in);