只想敲个代码
2019-03-05 22:56
Card类 public class Card implements Comparable<Card>{ public String cardColor; private String key; private String value; public String cardNum; public int num; public String getCardColor() { return cardColor; } public void setCardColor(String cardColor) { this.cardColor = cardColor; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Card() { } public Card(String cardColor,String cardNum,int num) { this.cardColor =cardColor; this.cardNum = cardNum; this.num = num; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getCardNum() { return cardNum; } public void setCardNum(String cardNum) { this.cardNum = cardNum; } @Override public int compareTo(Card o) { // TODO Auto-generated method stub return this.num-o.num; } } Player类 public class Player { public String ID; public String name; public List<Card> playerCards; public Player(String ID, String name) { this.ID = ID; this.name = name; this.playerCards = new ArrayList<Card>(); } public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Card> getPlayerCards() { return playerCards; } public void setPlayerCards(List<Card> playerCards) { this.playerCards = playerCards; } } CardGame类 import java.util.*; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class CardGame { public List<Card> cardList; public List<Player> playerList; private Scanner input; //Player p1; //Player p2; public CardGame() { this.cardList = new ArrayList<Card>(); this.playerList =new ArrayList<Player>(); this.input = new Scanner(System.in); } /** * 第一、要创建一副不包括大小王的扑克牌 * @param args */ public void Cards() { //创建扑克牌 System.out.println("--------------创建扑克牌—-----------------"); //用两个数组来存放扑克的花色和数值 String[] CardColor = new String[] {"黑桃","梅花","红桃","方块"}; String[] CardNum = new String[] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; //利用for循环取出扑克的花色和数值并且存放到CardList中 for(int i =0;i<4;i++) { for(int j=0;j<13;j++) { cardList.add(new Card(CardColor[i],CardNum[j],j*10+i)); } } //遍历打印扑克牌 System.out.println("--------------创建扑克牌成功—-----------------"); System.out.println("扑克牌为:"); for (Card card : cardList) { System.out.print(card.cardColor + card.cardNum +","); } System.out.println("共有52张牌"); //利用四个for循环来获取黑桃梅花红桃方块的对象,代码繁琐 //for(Integer i=2;i<11;i++) { //Card cd = new Card("黑桃",i.toString()); //cardList.add(cd); //} //Card cd1 = new Card("黑桃","A"); //cardList.add(cd1); //Card cd2 = new Card("黑桃","J"); //cardList.add(cd2); //Card cd3 = new Card("黑桃","Q"); //cardList.add(cd3); //Card cd4 = new Card("黑桃","K"); //cardList.add(cd4); } /** * 利用collections.shuffle方法来打乱List中扑克牌顺序 * @param args */ public void washCard() { System.out.println("--------------开始洗牌—-----------------"); Collections.shuffle(cardList); System.out.println("--------------结束洗牌—-----------------"); } /** * 创建玩家 * @param args */ public void players() { int i=0; for(;i<2;i++) { try { System.out.println("请输入第" + (i+1) +"位玩家的ID:"); String ID= input.next(); //提示输入玩家姓名 System.out.println("请输入第" + (i+1) +"位玩家的姓名:"); String name =input.next(); //创建新的玩家对象 Player newPlayer = new Player(ID,name); playerList.add(newPlayer); }catch(InputMismatchException e) { System.out.println("请输入正确的整数类型:"); } } } /** * 随机发牌 * @param args */ public void launchCard() { //随机给玩家发两张牌 System.out.println("--------------开始发牌—-----------------"); Random random = new Random(); int cardSize = cardList.size(); Card card = new Card(); for(int i=0;i<2;i++) { for (Player player : playerList) { card =cardList.get(random.nextInt(cardSize)); System.out.println("----玩家:"+ player.ID + ":" + player.name +"拿牌------"); player.playerCards.add(card); cardSize--; } } //p1.playerCards.add(cardList.get(0)); //System.out.println("----玩家:"+p1.name+"-拿牌------"); //p2.playerCards.add(cardList.get(1)); //System.out.println("----玩家:"+p2.name+"-拿牌------"); //p1.playerCards.add(cardList.get(2)); //System.out.println("----玩家:"+p1.name+"-拿牌------"); //p2.playerCards.add(cardList.get(3)); //System.out.println("----玩家:"+p2.name+"-拿牌------"); //Set<String> keySet = playerList.keySet(); //int i = 0; //int j = 0; //while (j < 2) { //for (String id : keySet) { //playerList.get(id).getPlayerCards().add(cardList.get(i)); //System.out.println("----玩家:" + playerList.get(id).getName() + "-拿牌"); // //} //j++; //} System.out.println("--------------发牌结束—-----------------"); } /** * 开始游戏 * @param args */ public void startGame() { System.out.println("--------------游戏开始—-----------------"); ArrayList<Card> maxCard = new ArrayList();//创建两玩家最大牌的集合 for (Player player : playerList ) { //利用Collections.sort进行排序 Collections.sort(player.getPlayerCards()); System.out.println(player.getName()+"最大的牌为:" + player.getPlayerCards().get(1).cardColor +player.getPlayerCards().get(1).cardNum); //输出玩家各自的手牌 System.out.println(player.getName()+"的牌为:" + player.getPlayerCards().get(0).cardColor +player.getPlayerCards().get(0).cardNum +"," + player.getPlayerCards().get(1).cardColor +player.getPlayerCards().get(1).cardNum); //将最大的牌存储 maxCard.add(player.getPlayerCards().get(1)); } //两玩家最大的牌进行比较 Collections.sort(maxCard); for (Player player : playerList) { if(player.getPlayerCards().contains(maxCard.get(1))) { System.out.println("恭喜玩家"+player.getName()+"获胜"); } } } public static void main(String[] args) { // TODO Auto-generated method stub CardGame cg = new CardGame(); cg.Cards(); cg.washCard(); cg.players(); cg.launchCard(); cg.startGame(); } }
创建玩家输入Id和姓名,我写了异常却不执行,不知道是什么问题???还是我写错了???
你并没有在catch中写出返回循环效果,那肯定不会返回呀。
在catch中做到循环效果就得在其使用try-catch语句外执行一次强制循环,因为continue返回效果只能用于循环体中,所以你可以测试在try-catch语句外面加 while(true){ ....}强制循环,然后再在catch中最后加入continue语句。
值得注意的是这样做虽然可以起到返回作用,但是Scanner输入的值并不会刷新,所以就会出现无限死循环。
所以应该在其continue语句前重新刷新Scanner值,也就是加入console=new Scanner(System.in);
Java入门第三季
409792 学习 · 4340 问题
相似问题