/*
*初始化一副扑克牌,顺序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/