求大神解答個問題,在寫的過程中我遇了這個問題 讓我十分不解 ,就是player player=new player();的問題!
在這類playingCcards下創(chuàng)建player一點(diǎn)問題沒有
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中,運(yùn)行就會提示報錯
(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("----------------創(chuàng)建撲克牌----------------"); 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"+"-------------撲克牌創(chuàng)建成功!--------------"); ????????/*** ?????????*?另外一種輸出方法?就不用了 ?????????*/ //????????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后面繼續(xù)寫代碼?while(true)才好使 while(true){ try{ sc=new?Scanner(System.in); playerID=sc.nextInt(); break; }catch(Exception?e){ System.out.println("必須為整數(shù)"); continue; } } System.out.println("請輸入第"+k+"位玩家姓名:"); playerName=sc.next(); playerList.add(new?player(playerID,playerName)); } //遍歷playerlist查看此時系統(tǒng)中有哪些玩家 for(int?p=0;p<playerList.size();p++){ System.out.println("目前存在的玩家有:"+playerList.get(p).id+ "??"+playerList.get(p).name); } } //發(fā)牌 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("目前已經(jīng)發(fā)出的牌有:"+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中最大的數(shù)據(jù)到底屬于誰! //下面這種寫法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(); } }
求大神解答 ?我好折磨死了
2016-01-08
第一個問題數(shù)組越界是因?yàn)閜layerList.get(0)報錯,你并沒有給playerList添加值????
第二個問題因?yàn)閜layer沒有編輯值所以player.playerOneCards.size()的值為初期值0