问答详情
源自:7-1 简易扑克牌游戏

求大神解答个问题,在写的过程中我遇了这个问题 让我十分不解 ,就是player player=new player();的问题!

在这类playingCcards下创建player一点问题没有

package com_poker_game;

import java.util.*;

public class playingCcards {
	player player=new player();//==============在这没问题========================
	List<Cards>  cardsList=new ArrayList<Cards>();
	List<player> playerList=new ArrayList<player>();
	String[] huaSe={"黑桃","方片","草花","红桃"};
	String[] daXiao={"2","3","4","5","6","7","8","9",
			 "10","J","Q","K","A"};
	

但是放在playingCcards 类中的方法sendPokers和compare中,运行就会提示报错

(Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1),而且compare中的

int size=player.playerOneCards.size();
System.out.println("lalal"+size);,size死活就是0。这是怎么回事呢??

package com_poker_game;

import java.util.*;

public class playingCcards {
	//player player=new player();=============不放这里了,这没了==============
	List<Cards>  cardsList=new ArrayList<Cards>();
	List<player> playerList=new ArrayList<player>();
	String[] huaSe={"黑桃","方片","草花","红桃"};
	String[] daXiao={"2","3","4","5","6","7","8","9",
			 "10","J","Q","K","A"};
	 
//	public void createCards(){
//		 for(int i=0;i<huaSe.length;i++){
//			 for(int j=0;i<daXiao.length;j++){
//				 cardsList.add(new Cards(huaSe[i],daXiao[j]));				 
//				//cardsSet输出所有扑克牌
//				 System.out.print(huaSe[i]+daXiao[j]+" ");
//			 }
//			 
//		 }
//		 
//	 }

	
	//建立扑克
	public void createCards() {
		System.out.println("----------------创建扑克牌----------------");
		for(int i = 0;i < huaSe.length;i++){
			 for(int j = 0;j < daXiao.length;j++){
				 cardsList.add(new Cards(huaSe[i],daXiao[j]));				 
				 System.out.print(huaSe[i]+daXiao[j]+" ");
			 }
			 
		 }
	        System.out.println("\n"+"-------------扑克牌创建成功!--------------");
	        /***
	         * 另外一种输出方法 就不用了
	         */
//        for (Cards card : cardsList) {
//           System.out.print(card.getType() + card.getNum() + ",");
//        }
        
        System.out.println("总共有" + cardsList.size() + "张牌。");
        
        //------------------------------洗牌
        Collections.shuffle(cardsList);
        System.out.println("--------------洗牌完成--------------");
    }
	
	
	//建立玩家
	public void creatPlayer(){
		int playerID=0;
		String playerName;
		Scanner sc=new Scanner(System.in);
			for(int k=1;k<=2;k++){
				System.out.println("请输入第"+k+"位玩家ID:");
				
				//while(true) 想要在catch后面继续写代码 while(true)才好使
				while(true){
					try{
						sc=new Scanner(System.in);
						playerID=sc.nextInt();
						break;
					}catch(Exception e){
						System.out.println("必须为整数");
						continue;				
					}	
							}
					System.out.println("请输入第"+k+"位玩家姓名:");
					playerName=sc.next();
					playerList.add(new player(playerID,playerName));
					
		}
			//遍历playerlist查看此时系统中有哪些玩家
			for(int p=0;p<playerList.size();p++){
			System.out.println("目前存在的玩家有:"+playerList.get(p).id+
					"  "+playerList.get(p).name);	
			}
	}
	
	
	//发牌
	public void sendPokers(){		
		player player=new player();//===============放这里=================
		int n=0;
		
		for(int u=0;u<4;u++){			
			//哈哈很喜欢这个方法 u==0+(2*n),再多人也不怕!!!!!
			if(u==0+(2*n)){
				player.playerOneCards.add(cardsList.get(u));
			}
			if(u==1+(2*n)){
				player.playerSecondCards.add(cardsList.get(u));
				//n++的位置很重要的啊!!!!!!!!!!!!!!!!
				n++;
			}
			
		}
//		for(int p=0;p<player.playerCards.size();p++){
//			System.out.println("目前已经发出的牌有:"+player.playerCards.get(p));	
//			}
		for(Cards pcr1:player.playerOneCards){
			System.out.println("玩家"+playerList.get(0).name+"的牌有:"
		+pcr1.type+pcr1.num);
		}
			for(Cards pcr2:player.playerSecondCards){
				System.out.println("玩家"+playerList.get(1).name+"的牌有:"
			+pcr2.type+pcr2.num);
		}
			
	}
	
	//比较两人手牌大小
	public void compare(){
		player player=new player();//===========在这也new一下=================
		List<Cards> everyoneMaxPokeList=new ArrayList<Cards>();
		Collections.sort(player.playerOneCards);
		Collections.sort(player.playerSecondCards);
		//crd.compareTo(per1.playerOneCards,per1.playerSecondCards);
		
		//获取手牌list的size
		int size=player.playerOneCards.size();
		//System.out.println("lalal"+size);
		
		//获得每个人最大的手牌
		System.out.println("玩家"+playerList.get(0).name+"的最大牌:"+player.playerOneCards.get(size-1).type+
				player.playerOneCards.get(size-1).num);
		System.out.println("玩家"+playerList.get(1).name+"的最大牌:"+player.playerSecondCards.get(size-1).type+
				player.playerSecondCards.get(size-1).num);
		
		//把每个人最大手牌放入everyoneMaxPokeList中再做比较
		everyoneMaxPokeList.add(player.playerOneCards.get(size-1));
		everyoneMaxPokeList.add(player.playerSecondCards.get(size-1));
		Collections.sort(everyoneMaxPokeList);
		
		int sze=everyoneMaxPokeList.size();

		//看看everyoneMaxPokeList中最大的数据到底属于谁!
		
		//下面这种写法equal方法有问题  待研究
//			if(everyoneMaxPokeList.get(sze-1).equals(player.playerOneCards)){
//				System.out.println("Winner is:"+playerList.get(0).name);
//			}else{
//				System.out.println("Winner is:"+playerList.get(1).name);
//			}
		if(player.playerOneCards.contains(everyoneMaxPokeList.get(sze-1))){
			System.out.println("Winner is:"+playerList.get(0).name);
		}else{
			System.out.println("Winner is:"+playerList.get(1).name);
		}	
	}
	 	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		playingCcards pc=new playingCcards();		
		pc.createCards();
		pc.creatPlayer();
		pc.sendPokers();
		pc.compare();
	}

}

求大神解答  我好折磨死了

提问者:泰兰德 2015-08-23 21:43

个回答

  • 刘铭218
    2016-01-08 00:24:13

    第一个问题数组越界是因为playerList.get(0)报错,你并没有给playerList添加值    

    第二个问题因为player没有编辑值所以player.playerOneCards.size()的值为初期值0