求大神幫我看看錯在哪,為什么會越界呢?
錯誤信息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at com.thomas.GameStart.initialize(GameStart.java:19)
at com.thomas.GameStart.main(GameStart.java:141)
源代碼
package com.thomas;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class GameStart {
String[] point=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] color=new String[]{"方片","梅花","紅桃","黑桃"};
Scanner input=new Scanner(System.in);
List<Poker> pokerList=new ArrayList<Poker>();
List<Player> playerList=new ArrayList<Player>();
public void initialize(){
for(int i=0;i<4;i++){
for(int j=0;i<13;j++){
pokerList.add(new Poker(color[i],point[j]));
}
}
System.out.println("-------------------創(chuàng)建撲克牌-----------------------");
int i=0;
for (Poker poker : pokerList) {
System.out.print(poker.color+poker.points+" ");
i++;
if(i%13==0){
System.out.println();
}
}
}
public void shuffle(){
System.out.println("-------------------開始洗牌-----------------------");
Collections.shuffle(pokerList);//打亂順序
System.out.println("-------------------洗牌完成-----------------------");
}
public void playSet(){
System.out.println("-------------------創(chuàng)建玩家-----------------------");
for(int i=1;i<=2;i++){
System.out.println("請輸入玩家"+i+"的id:");
int id=0;
try{
id=scanf();
}catch(Exception e){
System.out.println(e.getMessage());
i--;
continue;
}
System.out.println("請輸入玩家"+i+"的姓名:");
String name=input.next();
playerList.add(new Player(id,name));
}
input.close();//Scanner對象開啟了之后要記得關(guān)閉
System.out.println("-------------------Game Start!-----------------------\n紅方玩家+\t藍方玩家");
for (Player player : playerList) {
System.out.println(player.name+"\t");
}
}
public int scanf()throws Exception{
try{
int in=input.nextInt();
return in;
}catch(Exception e){
throw new Exception("輸入異常,請輸入整數(shù)類型的ID!");?
}
}
public void deal(){
System.out.println("-------------------開始發(fā)牌-----------------------");
System.out.println(playerList.get(0).name+"拿牌");
playerList.get(0).handCards.add(pokerList.get(0));
System.out.println(playerList.get(1).name+"拿牌");
playerList.get(0).handCards.add(pokerList.get(1));
System.out.println(playerList.get(0).name+"拿牌");
playerList.get(0).handCards.add(pokerList.get(2));
System.out.println(playerList.get(1).name+"拿牌");
playerList.get(0).handCards.add(pokerList.get(3));
System.out.println("-------------------發(fā)牌結(jié)束-----------------------");
}
public void sort(){
Collections.sort(playerList.get(0).handCards, new CompareToPoker());
Collections.sort(playerList.get(1).handCards, new CompareToPoker());
System.out.println(playerList.get(0).name+"最大手牌:"+playerList.get(0).handCards.get(0).color+playerList.get(0).handCards.get(0).points);
System.out.println(playerList.get(0).name+"最大手牌:"+playerList.get(1).handCards.get(0).color+playerList.get(1).handCards.get(0).points);
}
public void compareToPoint(){
System.out.println("-------------------獲勝方-----------------------");
List<Poker> maxPoker=new ArrayList<Poker>();
List<Poker> minPoker=new ArrayList<Poker>();
maxPoker.add(playerList.get(0).handCards.get(0));
maxPoker.add(playerList.get(1).handCards.get(0));
minPoker.add(playerList.get(0).handCards.get(1));
minPoker.add(playerList.get(1).handCards.get(1));
Collections.sort(maxPoker, new CompareToPoker());
Collections.sort(minPoker,new CompareToPoker());
if(maxPoker.get(0).points.equals(maxPoker.get(1).points)){
if(minPoker.get(0).points.equals(minPoker.get(1).points)){
if(maxPoker.get(0).equals(playerList.get(0).handCards.get(0))){?
System.out.println("紅方獲勝\tWinner:"+playerList.get(0).name);?
}else{?
System.out.println("藍方獲勝\tWinner:"+playerList.get(1).name);?
}?
}else{?
if(minPoker.get(0).equals(playerList.get(0).handCards.get(1))){
System.out.println("紅方獲勝\tWinner:"+playerList.get(0).name);
}else{
System.out.println("藍方獲勝\tWinner:"+playerList.get(1).name);
}?
}
}else if(maxPoker.get(0).equals(playerList.get(0).handCards.get(0))){
System.out.println("紅方獲勝\tWinner:"+playerList.get(0).name);
}else{?
System.out.println("藍方獲勝\tWinner:"+playerList.get(1).name);
}
}
public void print(){?
System.out.println("----------玩家各自手牌-------------");?
for(Player player : playerList){?
System.out.print(player.name+":");?
for(Poker poker : player.handCards){?
System.out.print("["+poker.color+poker.points+"]");?
}?
System.out.println("");?
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("----------歡迎進入紙牌大戰(zhàn)-------------");
GameStart gs=new GameStart();
gs.initialize();
gs.shuffle();
gs.playSet();
gs.deal();
gs.sort();
gs.compareToPoint();
gs.print();
System.out.println("----------游戲結(jié)束-------------");
}
}
2017-02-12
initialize()的第二重循環(huán)寫錯了
求采納!
2017-02-18
對對對,一快就敲錯了,謝謝