代碼問題 求解! Comparator
完成了大體,如下
import?java.util.ArrayList; import?java.util.Arrays; import?java.util.Collections; import?java.util.HashSet; import?java.util.InputMismatchException; import?java.util.List; import?java.util.Scanner; import?java.util.Set; public?class?Test?{ List<Player>?playerList; List<Poker>?tempPoker;//只為在洗牌前順序顯示一次,看起來多余 Set<Poker>?pokerToDeal; Scanner?input; public?Test(){ this.playerList=new?ArrayList<Player>(); this.tempPoker=new?ArrayList<Poker>(); this.pokerToDeal=new?HashSet<Poker>(); input=new?Scanner(System.in); } //創(chuàng)建一副撲克牌(不含大小王),洗牌 public?void?addPokerToDeal(){ System.out.println("---------創(chuàng)建撲克牌--------"); Poker[]?poker={ new?Poker("黑桃","2"),new?Poker("黑桃","3"),new?Poker("黑桃","4"),new?Poker("黑桃","5"), new?Poker("黑桃","6"),new?Poker("黑桃","7"),new?Poker("黑桃","8"),new?Poker("黑桃","9"), new?Poker("黑桃","10"),new?Poker("黑桃","J"),new?Poker("黑桃","Q"),new?Poker("黑桃","K"),new?Poker("黑桃","A"), new?Poker("紅桃","2"),new?Poker("紅桃","3"),new?Poker("紅桃","4"),new?Poker("紅桃","5"), new?Poker("紅桃","6"),new?Poker("紅桃","7"),new?Poker("紅桃","8"),new?Poker("紅桃","9"), new?Poker("紅桃","10"),new?Poker("紅桃","J"),new?Poker("紅桃","Q"),new?Poker("紅桃","K"),new?Poker("紅桃","A"), new?Poker("梅花","2"),new?Poker("梅花","3"),new?Poker("梅花","4"),new?Poker("梅花","5"), new?Poker("梅花","6"),new?Poker("梅花","7"),new?Poker("梅花","8"),new?Poker("梅花","9"), new?Poker("梅花","10"),new?Poker("梅花","J"),new?Poker("梅花","Q"),new?Poker("梅花","K"),new?Poker("梅花","A"), new?Poker("方塊","2"),new?Poker("方塊","3"),new?Poker("方塊","4"),new?Poker("方塊","5"), new?Poker("方塊","6"),new?Poker("方塊","7"),new?Poker("方塊","8"),new?Poker("方塊","9"), new?Poker("方塊","10"),new?Poker("方塊","J"),new?Poker("方塊","Q"),new?Poker("方塊","K"),new?Poker("方塊","A"),}; tempPoker.addAll(Arrays.asList(poker)); System.out.println("---------撲克牌創(chuàng)建成功!----------\n"+"分別為:"); for?(Poker?pk?:?tempPoker)?{ System.out.print(pk.prefix+pk.ch+","); } System.out.println("--------開始洗牌。。。----------"); pokerToDeal.addAll(Arrays.asList(poker)); System.out.println("--------洗牌結(jié)束??!-----------"); /*for?(Poker?pk?:?pokerToDeal)?{ System.out.println(pk.prefix+pk.ch); }*/ } //創(chuàng)建兩名玩家 public?void?playerAndDeal(){ try?{ for(int?i=1;i<3;i++){ System.out.println("請(qǐng)輸入第"+i+"位玩家的ID和姓名:"); System.out.println("輸入ID:"); int?id=input.nextInt(); System.out.println("輸入姓名:"); String?name=input.next(); Player?player=new?Player(id,name); playerList.add(player); } }?catch?(InputMismatchException?e)?{ //?TODO?Auto-generated?catch?block System.out.println("ID必須為數(shù)字!"); input.next(); playerAndDeal(); }?catch?(Exception?e)?{ e.printStackTrace(); } Player?p1=playerList.get(0); Player?p2=playerList.get(1); System.out.println("歡迎玩家:"+p1.name); System.out.println("歡迎玩家:"+p2.name); System.out.println("---------開始發(fā)牌...---------------"); List<Poker>?pokerList=new?ArrayList<Poker>(pokerToDeal); System.out.println("玩家:"+p1.name+"-拿牌"); p1.ownPoker.add((Poker)?pokerList.get(0)); System.out.println("玩家:"+p2.name+"-拿牌"); p2.ownPoker.add((Poker)?pokerList.get(1)); System.out.println("玩家:"+p1.name+"-拿牌"); p1.ownPoker.add((Poker)?pokerList.get(2)); System.out.println("玩家:"+p2.name+"-拿牌"); p2.ownPoker.add((Poker)?pokerList.get(3)); System.out.println("---------發(fā)牌結(jié)束!--------"); System.out.println("-----------開始游戲。。。--------------"); List<Poker>?ownPokerList1=new?ArrayList<Poker>(p1.ownPoker); Poker?p1max; if(ownPokerList1.get(0).ch.equals(ownPokerList1.get(1).ch)){ Collections.sort(ownPokerList1,?new?PokerComparator()); p1max=ownPokerList1.get(1); }else{ Collections.sort(ownPokerList1); p1max=ownPokerList1.get(1); } System.out.println("玩家:"+p1.name+"最大的手牌為:"+p1max.prefix+p1max.ch); List<Poker>?ownPokerList2=new?ArrayList<Poker>(p2.ownPoker); Poker?p2max; if(ownPokerList2.get(0).ch.equals(ownPokerList2.get(1).ch)){ Collections.sort(ownPokerList2,?new?PokerComparator()); p2max=ownPokerList2.get(1); }else{ Collections.sort(ownPokerList2); p2max=ownPokerList2.get(1); } System.out.println("玩家:"+p2.name+"最大的手牌為:"+p2max.prefix+p2max.ch); List<Poker>?newList=new?ArrayList<Poker>(); newList.add(p1max); newList.add(p2max); Poker?maxPoker; if(newList.get(0).ch.equals(newList.get(1).ch)){ Collections.sort(newList,?new?PokerComparator()); maxPoker=newList.get(1); }else{ Collections.sort(newList); maxPoker=newList.get(1); } if(maxPoker.equals(p1max)) System.out.println("獲勝玩家:"+p1.name); if(maxPoker.equals(p2max)) System.out.println("獲勝玩家:"+p2.name); System.out.println("玩家各自的手牌為:"); System.out.println(p1.name+":"+ownPokerList1.get(0).prefix+ownPokerList1.get(0).ch +","+ownPokerList1.get(1).prefix+ownPokerList1.get(1).ch); System.out.println(p2.name+":"+ownPokerList2.get(0).prefix+ownPokerList2.get(0).ch +","+ownPokerList2.get(1).prefix+ownPokerList2.get(1).ch); } /* ????public?void?go(Player?p1,Player?p2){ System.out.println("-----------開始游戲。。。--------------"); List<Poker>?ownPokerList1=new?ArrayList<Poker>(p1.ownPoker); Poker?p1max; if(ownPokerList1.get(0).ch.equals(ownPokerList1.get(1).ch)){ Collections.sort(ownPokerList1,?new?PokerComparator()); p1max=ownPokerList1.get(1); }else{ Collections.sort(ownPokerList1); p1max=ownPokerList1.get(1); } System.out.println("玩家:"+p1.name+"最大的手牌為:"+p1max.prefix+p1max.ch); List<Poker>?ownPokerList2=new?ArrayList<Poker>(p2.ownPoker); Poker?p2max; if(ownPokerList2.get(0).ch.equals(ownPokerList2.get(1).ch)){ Collections.sort(ownPokerList2,?new?PokerComparator()); p2max=ownPokerList2.get(1); }else{ Collections.sort(ownPokerList2); p2max=ownPokerList2.get(1); } System.out.println("玩家:"+p2.name+"最大的手牌為:"+p2max.prefix+p2max.ch); List<Poker>?newList=new?ArrayList<Poker>(); newList.add(p1max); newList.add(p2max); Poker?maxPoker; if(newList.get(0).ch.equals(newList.get(1).ch)){ Collections.sort(newList,?new?PokerComparator()); maxPoker=newList.get(1); }else{ Collections.sort(newList); maxPoker=newList.get(1); } if(maxPoker.equals(p1max)) System.out.println("獲勝玩家:"+p1.name); if(maxPoker.equals(p2max)) System.out.println("獲勝玩家:"+p2.name); System.out.println("玩家各自的手牌為:"); System.out.println(p1.name+":"+ownPokerList1.get(0).prefix+ownPokerList1.get(0).ch +","+ownPokerList1.get(1).prefix+ownPokerList1.get(1).ch); System.out.println(p2.name+":"+ownPokerList2.get(0).prefix+ownPokerList2.get(0).ch +","+ownPokerList2.get(1).prefix+ownPokerList2.get(1).ch); }*/ public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub Test?t=new?Test(); t.addPokerToDeal(); t.playerAndDeal(); } } ======================================================================================================= import?java.util.HashSet; import?java.util.Set; public?class?Player?{ public?int?id; public?String?name; public?Set<Poker>?ownPoker; public?Player(int?id,String?name){ this.id=id; this.name=name; this.ownPoker=new?HashSet<Poker>(); } } ======================================================================================================== public?class?Poker?implements?Comparable<Poker>{ public?String?prefix; public?String?ch; public?Poker(String?prefix,?String?ch){ this.prefix=prefix; this.ch=ch; } @Override public?int?hashCode()?{ final?int?prime?=?31; int?result?=?1; result?=?prime?*?result?+?((ch?==?null)???0?:?ch.hashCode()); result?=?prime?*?result?+?((prefix?==?null)???0?:?prefix.hashCode()); return?result; } @Override public?boolean?equals(Object?obj)?{ if?(this?==?obj) return?true; if?(obj?==?null) return?false; if?(!(obj?instanceof?Poker)) return?false; Poker?other?=?(Poker)?obj; if?(ch?==?null)?{ if?(other.ch?!=?null) return?false; }?else?if?(!ch.equals(other.ch)) return?false; if?(prefix?==?null)?{ if?(other.prefix?!=?null) return?false; }?else?if?(!prefix.equals(other.prefix)) return?false; return?true; } @Override public?int?compareTo(Poker?o)?{ //?TODO?Auto-generated?method?stub return?this.ch.compareTo(o.ch); } }
按照我的思路,需要?jiǎng)?chuàng)建一個(gè)類PokerComparator用以比較黑、紅、梅、方
我暫時(shí)如上寫免于報(bào)錯(cuò)
如何更改或者補(bǔ)全呢?
另外,addPokerToDeal()中的一部分內(nèi)容,我曾試圖移到go()中去,然后調(diào)用
結(jié)果失敗了,只好注釋掉,見注釋部分
public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub Test?t=new?Test(); t.addPokerToDeal(); t.playerAndDeal(); t.go(p1,?p2);???這句不行 }
因?yàn)檎{(diào)用時(shí)參數(shù)有問題,t.go()怎么寫都不對(duì),怎樣才能完成調(diào)用?
2016-07-31
你最好按照代碼語言(選擇JAVA),分開來發(fā)上來,這樣看的太亂了,你也沒說你有什么錯(cuò)誤啊,或者運(yùn)行報(bào)了什么錯(cuò)?如果不能運(yùn)行的話,那就是代碼還沒完成,繼續(xù)修改,可以參照下評(píng)論里的,我就是參照了下評(píng)論的,然后代碼優(yōu)化了很多