Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at 简易扑克.Card.getCard(Card.java:45) at 简易扑克.Game.main(Game.java:47) 目前我只做了一会会..只实现了部分功能...但是一直卡在这个异常...希望各位可以帮帮我~谢谢 package 简易扑克; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Player { public String name; public List<Card> cardGet; public Player(String name){ this.name=name; cardGet=new ArrayList<Card>(); } public Player(){ } }
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class Game { Scanner input; public List<Player> players; public Game(){ this.players=new ArrayList<Player>(); } public void playerCreat(){ System.out.println("-------开始创建玩家-------"); input=new Scanner(System.in); System.out.println("请输入玩家人数:"); try{ int count=input.nextInt(); for(int i=0;i<count;i++){ System.out.println("请输入姓名:"); String name=input.next(); Player tmpPlayer=new Player(name); players.add(i,tmpPlayer); System.out.println("--------成功创建玩家:"+tmpPlayer.name+"----------"); } }catch(Exception e){ System.out.println("请输入整数!!!!"); playerCreat(); } } public void showPlayer(){ for (Player player : players) { System.out.println(player.name); } } public static void main(String[] args) { Card c=new Card(); Game g=new Game(); c.creatCard(); c.showCard(); c.washCard(); g.playerCreat(); g.showPlayer(); c.getCard(); } }
package 简易扑克; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.Scanner; public class Card { String colour; String number; public String[] num={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; public String col[]={"黑桃","红桃","梅花","方片"}; List<Card> cards; public Card(){ this.cards=new ArrayList<Card>(); } public Card(String colour,String number){ this.colour=colour; this.number=number; } public void creatCard(){ for(int i=0;i<col.length;i++){ for(int j=0;j<num.length;j++){ cards.add(new Card(col[i],num[j])); } } } public void showCard(){ System.out.println("----------获得扑克牌----------"); for (int i=0;i<cards.size();i++) { System.out.print(cards.get(i).colour+cards.get(i).number+","); } System.out.println(""); } public void washCard(){ System.out.println("----------开始洗牌--------"); Collections.shuffle(cards); System.out.println("----------洗牌结束--------"); } public void getCard(){ Game ga=new Game(); for(int i=0;i<2;i++){ System.out.println("玩家:"+ga.players.get(i).name+"抓牌"); ga.players.get(i).cardGet.add(cards.get(i)); } } }
相关分类