import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
* 模拟洗牌
*/
public class PokerDemo {
// 定义数组,表示花色和牌值
private String[] types = { "方块", "草花", "红心", "黑桃" };
private String[] values = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A" };
// 定义一个用于存储扑克牌的集合
public List<String> list = new ArrayList<String>();
// 把牌给装到牌盒里
public void init() {
for (int x = 0; x < types.length; x++) {
for (int y = 0; y < values.length; y++) {
list.add(types[x] + values[y]);
}
}
}
public static void main(String[] args) {
PokerDemo pd = new PokerDemo();
// 调用初始化方法,把牌放到牌盒
pd.init();
// 洗牌
System.out.println("洗牌前:" + pd.list);
Collections.shuffle(pd.list);
System.out.println("洗牌前:" + pd.list);
}
}
自己写的模拟洗牌程序,看看对你有没有帮助 |