A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 廉伟 中级黑马   /  2012-9-29 23:55  /  1110 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Poker
  2. {
  3.         Poker()
  4.         {
  5.                
  6.         }
  7.         Poker(String color,String number)
  8.         {
  9.                 this.color=color;
  10.                 this.number=number;

  11.         }
  12.         public void setColor(String color)
  13.         {
  14.                 this.color=color;
  15.         }
  16.         public void setNumber(String number)
  17.         {
  18.                 this.number=number;
  19.         }
  20.         public String getColor()
  21.         {
  22.                 return this.color;

  23.         }
  24.         public String getNumber()
  25.         {
  26.                 return this.number;
  27.         }

  28.         public static void main(String[] args)
  29.         {
  30.                 Poker [] p=new Poker [PokerTools.cs.length*PokerTools.ns.length];
  31.                 init(p);
  32.                 printArray(p);
  33.                 System.out.println("随机输入");
  34.                 getPoker(p);
  35.                 printArray(p);
  36.         }
  37.         public static void init(Poker [] p)
  38.         {
  39.                         int index=0;
  40.                         for (int i=0;i<PokerTools.cs.length ;i++ )
  41.                         {
  42.                                 for (int j=0;j<PokerTools.ns.length;j++ )
  43.                                 {
  44.                                         p[index++]=new Poker(PokerTools.cs[i],PokerTools.ns[j]);
  45.                                 }
  46.                         }
  47.         }
  48.         public static Poker [] getPoker(Poker [] p)
  49.         {
  50.                 for (int x=0;x<p.length ;x++ )
  51.                 {
  52.                         int p1=(int)(Math.random()*52);
  53.                         int p2=(int)(Math.random()*52);
  54.                         Poker temp=p[p1];
  55.                         p[p1]=p[p2];
  56.                         p[p2]=temp;
  57.                 }
  58.                 return p;
  59.                
  60.         }
  61.         public static void printArray(Poker [] p)
  62.         {
  63.                 for (int x=0;x<p.length ;x++ )
  64.                 {
  65.                         if (x%13==0)
  66.                         {
  67.                                 System.out.println();
  68.                         }
  69.                         System.out.print(p[x].getColor()+p[x].getNumber()+"  ");
  70.                 }
  71.         }
  72. }
  73. class PokerTools
  74. {
  75.         public static String[] cs={"黑桃","红桃","梅花","方块"};//静态成员
  76.         public static String[] ns={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
  77. }
复制代码
第二种
  1. public class PuKeFaPai {
  2.     public static void main(String [] args){
  3.          String[] huase=new String[]{"红桃","方块","黑桃","梅花"};
  4.   String[] paihao=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
  5.   int []paizhi=new int[]{2,3,4,5,6,7,8,9,10,11,12,13,14};
  6.   String[] pai=new String [52];
  7.   String[] PaiHao=new String [52];
  8.    String[] HuaSe=new String [52];
  9.    int []PaiZhi=new int[52];
  10.   int i=0,j=0;
  11.   int k=0,m=0,n=0,p=0,s=0;
  12.   int count=0;
  13.   int a;
  14.   int b=0;
  15.   String str="";
  16.   public void FaPai()
  17.           {
  18.   for(i=0;i<4;i++){
  19.    for(j=0;j<13;j++){
  20.     pai[k]=huase[i].concat(paihao[j]);
  21.     PaiZhi[s]=paizhi[j];
  22.     k++;
  23.     s++;
  24.     }
  25.    }
  26.   
  27.   while(count<51){
  28.   do{
  29.    a=(int)(Math.random()*(52-count));
  30.    str=pai[a];
  31.    pai[a]=pai[51-count];
  32.    pai[51-count]=str;
  33.    b=PaiZhi[a];
  34.    PaiZhi[a]=PaiZhi[51-count];
  35.    PaiZhi[51-count]=b;
  36.       count++;
  37.    }while(count<51);
  38.   }for(i=0;i<52;i++){
  39.    if(pai[i].length()<4){
  40.    PaiHao[i]=pai[i].substring(2,3);
  41.   }else if(pai[i].length()==4){
  42.    PaiHao[i]=pai[i].substring(2,4);
  43.   }
  44.   HuaSe[i]=pai[i].substring(0, 2);
  45. }
  46.   }  
  47. }

  48. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马