这是我以前写的一个随机抽取5张扑克牌程序,你参考一下吧:import java.util.*;public class Card{
public static void main(String[] args){
String[] cardType={"clubs","diamonds","spades","hearts"};//牌色
String[] cardNum={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};//排数
ArrayList card=new ArrayList();//新建一副牌
for(int i=0;i<cardType.length;i++){
for(int j=0;j<cardNum.length;j++){
card.add(cardType[i]+"_"+cardNum[j]);
}
}
card.add("big_King");//添加大小王
card.add("small_King");
for(int i=0;i<5;i++){//随机发5张牌
int index=readCard(card);
card.remove(index);
}
}
public static int readCard(ArrayList al){//读牌
Random rand=new Random();
int randNumber=rand.nextInt(al.size());
String card=al.get(randNumber).toString();
System.out.println (chinaShow(card));
return randNumber;
}
public static String chinaShow(String card){//牌色中文显示
StringBuffer buffer=new StringBuffer();
String[] thisCard=card.split("_");
String cardType=thisCard[0];
String cardNum=thisCard[1];
if(cardType.equals("clubs")){
buffer.append("梅花");
}
else if(cardType.equals("diamonds")){
buffer.append("方块");
}
else if(cardType.equals("spades")){
buffer.append("黑桃");
}
else if(cardType.equals("hearts")){
buffer.append("红心");
}
else if(cardType.equals("big")){
buffer.append("大王");
}
else if(cardType.equals("small")){
buffer.append("小王");
}
return buffer.append(cardNum).toString();
}
}
温馨提示:内容为网友见解,仅供参考