public static List <String> Pokers= new ArrayList <String> ();
里面是一副牌内容如 方片A, 红桃8, 方片4, 红桃4, 红桃9, 梅花3, 梅花K, 红桃3, 梅花8, 黑桃6,大鬼小鬼。
求一个规则谢谢,小弟学习了

解决方案 »

  1.   

    很显然你不能直接把“方片A”存到 List 中去!
      

  2.   

    把牌封装成一个类Card,有属性rank(点数)和suit(花色)
      

  3.   

    嘿,很久很久以前的课程设计里面有这个,记得当时还封装了洗牌(flush()),随机发牌(random())的方法,哈哈
      

  4.   

    comparable 里面先比大小 大小一样再比花色。 还是自己动手好啊,楼主 这东西不难
      

  5.   

    同意8楼,把扑克牌定义成一个JavaBean,其中必须两个属性:大小、花色然后再在compareTo方法中比较,最后使用Collections.sort(list)就可以排序了
      

  6.   

    package justforjoke.pkGame;
    //花色
    public class CardsType {
     private static final String [] ct={
      "黑桃",
      "红桃",
      "梅花",
      "方块"
     };
     
     public static String getType(int i){
      return ct[i];
     }
     
     public static int com(String s,String c){
      int s1=find(s);
      int s2=find(c);
      if(s1==-1)return 100;
      if(s2==-1)return -100;
      return s2-s1;
     }
     
     private static int find(String s){
      int i=-1;
      for(String st:ct ){
       i++;
       if(st.equals(s))return i;
      }
      return i;
     }
    }
    package justforjoke.pkGame;
    //牌码
    public class Num {
     private static final String[]n={
      "0","2","3","4","5","6","7","8","9","10","J","Q","K","A"
     };
     
     public static String getN(int i){
      if(i<1||i>13)return null;
      return n[i];
     }
    }
    package justforjoke.pkGame;
    //每张牌
    public class Cards implements Comparable{
     private String type;
     private int num;
     
     public Cards(){}
     public Cards(String s,int n){
      this.type=s;
      this.num=n;
     }
     public String getType() {
      return type;
     }
     public void setType(String type) {
      this.type = type;
     }
     public int getNum() {
      return num;
     }
     public void setNum(int num) {
      this.num = num;  
     }
     
     public boolean equals(Object o){  
      if(!(o instanceof Cards))return false;
      Cards c=(Cards)o;
      if(this.num==c.getNum()&&this.type.equals(c.getType()))return true;
      return false;
     }
     public int compareTo(Object o) {
      if(!(o instanceof Cards))return 1;
      Cards c=(Cards)o;
      int bjjg;
      if((bjjg=CardsType.com(this.type,c.getType()))!=0)return bjjg;  
      return c.getNum()-this.num;
     }
     
     public String toString(){ 
      return type+" : "+Num.getN(num);
      
     }}
    package justforjoke.pkGame;
    //一副新扑克
    public class PkC {
     public static Cards[] getNewCards(){
      Cards[] nc=new Cards[52];
      int k=0;
      for(int i=0;i<4;i++){
       String t=CardsType.getType(i);
       for(int j=1;j<14;j++){
        nc[k++]=new Cards(t,j);    
       }
      }
      return nc;
     }
    }
    package justforjoke.pkGame;import java.util.Arrays;
    import java.util.Random;
    //游戏,包含两幅扑克
    public class Game {
     private Cards[]gc=new Cards[104];
     private int[] num=new int[104]; 
     private int length=103;
     public Game(){
      int i=0;
      for(Cards c:PkC.getNewCards()){
       gc[i++]=c;
      }
      for(Cards c:PkC.getNewCards()){
       gc[i++]=c;
      }
      for(int j=0;j<104;j++)num[j]=j;
     } 
     //发牌
     public Cards[] fp(){
      Cards[] pf=new Cards[5];
      Random r=new Random();
      for(int i=0;i<5;i++){
       if(length<1)break;
       int t=r.nextInt(length);
       int tem=num[t];
       num[t]=num[length];
       num[length--]=tem;
       pf[i]=gc[tem];
      }  
      return pf;
     }
    //去掉函数中的注释会打印游戏的中间过程
     public static String cp(Cards[] c){
      String result1="同花";
      String result2="顺子";
      String result3="";
      for(int i=0;i<4;i++){   
       if(!c[i].getType().equals(c[i+1].getType()))result1="";
       if(c[i+1].getNum()-c[i].getNum()!=1)result2="";
       if(c[i].equals(c[i+1]))result3="对子";
    //System.out.print(c[i]+" ||  ");
      }
    //System.out.println(c[4]);
      return result1+result2+result3;
     }
     public static void main(String []args){
      Game g=new Game();
      while(true){
       Cards[] pf=g.fp();
       if(pf[4]==null){
        System.out.println("游戏失败!!");
        break;
       }
       Arrays.sort(pf);
       String r=Game.cp(pf);
       if(!r.equals("")){
        
        System.out.println(r);
        for(Cards cs:pf)System.out.print(cs+"  ||  ");
        break;
       }
       
       
      }
     }}