Java做洗牌

 

/*
 *初始化一副扑克牌,顺序A-K,黑桃,梅花,红桃,方块
 *并提供洗牌方法mixCard,随机洗牌
 */
public class Card{
 private String face;
 private String suit;

 String [] f = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
 String [] s = {"黑桃","梅花","红桃","方块"};


 public Card(){
 }

 public Card(String face, String suit){
  this.face = face;
  this.suit = suit;
 }

 protected String  getSuit(){
  return this.suit;
 }

 protected String getFace(){
  return this.face;
 }

 public String[] init(){
  String [] temp = new String[52];
  for(int i=0; i<s.length; i++){
   for(int j=0; j<f.length; j++){
    temp[i*13+j] = this.s[i]+":"+this.f[j];
   }
  }
  return temp;
 }

 public String[] mixCard(String[] card){
  String [] mix = new String [52];
  int flag = 0;
  int index = 0;
  while(true){
   index =  (int)(Math.random()*52);
   if(mix[index] == null){
    mix[index] = card[flag];
    flag++;
   }
   if(flag > 51){
    break;
   }
  }
  return mix;
  
 }

 public String toString(){
  return this.face + " of "+ this.suit;
 }
 
}


public class Test{
 public static void main(String [] args){
  String desk[] = new String[52];
  Card c = new Card();
  desk = c.init();
  for(String s : desk){
   System.out.print(s+" ");
  }
  System.out.println(" ");
  
  desk = c.mixCard(desk);
  for(String s : desk){
   System.out.print(s+" ");
  }
  System.out.println(" ");
 }
}

来自:http://chenhongbin007.blog.163.com/blog/static/3406992120093278914770/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值