import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PokerDemo {
//在PokerDemo类中建立两个成员属性,一个控制花色,一个控制牌符
private String [] type={"黑桃","红桃","梅花","方片"};
private String [] val={"2","3","4","5",
"6","7","8","9",
"10","J","Q","K","A"};
//然后将其存入ArrayList集合当中,遍历集合,相当于装牌动作
public List<String> list=new ArrayList<String>();
public void init(){
for(int i=0;i<type.length;i++){
for(int j=0;j<val.length;j++){
list.add(type[i]+val[j]);
}
}
list.add("大王");
list.add("小王");
}
public static void main(String[] args) {
PokerDemo pd=new PokerDemo();
pd.init();
System.out.println("洗牌了"+pd.list);
//采用集合工具类的Collections的shuffle方法洗牌
Collections.shuffle(pd.list);
//输出洗牌后的结果
System.out.println("洗牌后"+pd.list);
}
}
本人觉得用集合工具类就可以解决,而且很方便! |