public class PokerGame {?? ?List<Poker> pokers = new ArrayList<>();?? ?public PokerGame() {?? ??? ?int count = 13;?? ??? ?for(int i = 14; i > 1 ; i--,count--){?? ??? ??? ?String pString = i+"";?? ??? ??? ?if (i == 11){?? ??? ??? ??? ?pString = "J";?? ??? ??? ?}?? ??? ??? ?if (i == 12){?? ??? ??? ??? ?pString = "Q";?? ??? ??? ?}?? ??? ??? ?if (i == 13){?? ??? ??? ??? ?pString = "K";?? ??? ??? ?}?? ??? ??? ?if (i == 14){?? ??? ??? ??? ?pString = "A";?? ??? ??? ?}?? ??? ??? ?Poker[] spade = new Poker[count];?? ??? ??? ?//《錯誤日志》上面申請的是count長度的數(shù)組,但是數(shù)組是從零開始的,所以最大的數(shù)組下標(biāo)應(yīng)該是count減一?? ??? ??? ?spade[count-1] = new Poker();?? ??? ??? ?spade[count-1].setPokerFaces(pString);?? ??? ??? ?spade[count-1].setFlowerColor("黑桃");?? ??? ??? ?pokers.add(spade[count-1]);?? ??? ?}?? ??? ?for(Poker p:pokers){?? ??? ??? ?System.out.println(p.getFlowerColor()+p.getPokerFaces());?? ??? ?}?? ?}?? ??? ?public void playGame(){?? ??? ?Player p1 = new Player();?? ??? ?@SuppressWarnings("resource")?? ??? ?Scanner input = new Scanner(System.in);?? ??? ?System.out.println("請輸入第一位玩家的姓名:");?? ??? ?p1.setName(input.next());?? ??? ?Player p2 = new Player();?? ??? ?@SuppressWarnings("resource")?? ??? ?Scanner input2 = new Scanner(System.in);?? ??? ?System.out.println("請輸入第二位玩家的姓名:");?? ??? ?p2.setName(input2.next());?? ??? ??? ??? ?Random random = new Random();?? ??? ?int r = (random.nextInt(12));?? ??? ?String p1hand = pokers.get(r).getFlowerColor() + pokers.get(r).getPokerFaces();?? ??? ?p1.setHandPoker(p1hand);?? ??? ?pokers.remove(r);?? ??? ??? ??? ?Random random2 = new Random();?? ??? ?int r2 = (random2.nextInt(12));?? ??? ?String p2hand = pokers.get(r2).getFlowerColor() + pokers.get(r2).getPokerFaces();?? ??? ?p2.setHandPoker(p2hand);?? ??? ?pokers.remove(r2);?? ??? ??? ??? ?if(r < r2){?? ??? ??? ?System.out.println("玩家" + p1.getName() + "獲得勝利!");?? ??? ?}else {?? ??? ??? ?System.out.println("玩家" + p2.getName() + "獲得勝利!");?? ??? ?}?? ??? ??? ??? ?System.out.println("玩家" + p1.getName() + "的手牌是:" + p1.getHandPoker());?? ??? ?System.out.println("玩家" + p2.getName() + "的手牌是:" + p2.getHandPoker());?? ?}?? ??? ?public static void main(String[] args) {?? ??? ?PokerGame pokerGame? = new PokerGame();?? ??? ?pokerGame.playGame();?? ?}}
運行幾十次,總會出一次錯誤的比較大小
Meng7y
2017-07-07 20:24:29