package pokerGame; import java.util.List; public class Player { private Integer id; private String name; private List<Poker> playerPokerList; public List<Poker> getPlayerPokerList() { return playerPokerList; } public void setPlayerPokerList(List<Poker> playerPokerList) { this.playerPokerList = playerPokerList; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package pokerGame; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class PokerGame { public List<Poker> pokerList; public Map<Integer,List> playersPokerList; public Player [] players; public PokerGame(){ pokerList=new ArrayList<Poker>();//一副扑克牌 playersPokerList=new HashMap<Integer,List>();//玩家手牌集合 players=new Player[2]; } //创建玩家 public void createPlayer(){ System.out.println("------------------创建玩家---------------------"); Scanner console=new Scanner(System.in); Integer id; for(int i=0;i<2;i++){ System.out.println("请输入第"+(i+1)+"位玩家ID和姓名"); while(true){ try{ System.out.println("输入ID:"); id=console.nextInt(); players[i].setId(id); break; }catch(Exception e){ System.out.println("输入类型不匹配,请输入一个整数型ID!!!"); console=new Scanner(System.in); continue; } } System.out.println("输入姓名:"); String name=console.next(); players[i].setName(name); } for(int j=0;j<2;j++){ System.out.println("---欢迎玩家:"+players[j].getName()); } } public static void main(String[] args) { // TODO Auto-generated method stub PokerGame pokerGame=new PokerGame(); pokerGame.createPlayer(); } }
你的try语句把int类型当异常捕获了