import java.util.LinkedList;
import java.util.Random;
class Poker {
String color;
String point;
public Poker(String color, String point) {
super();
this.color = color;
this.point = point;
}
@Override
public String toString() {
return "{" + color + point + "}";
}
}
public class Puke {
public static void main(String[] args) {
LinkedList list = getPuke();
changePuke(list);
showPuke(list);
}
// 交换扑克牌的位置
public static void changePuke(LinkedList list) {
// 创建随机数对象
Random random = new Random();
for (int i = 0; i < 100; i++) {
// 随机产生两个索引值
int index1 = random.nextInt(list.size());
int index2 = random.nextInt(list.size());
// 根据索引值去除两张牌
Poker p1 = (Poker) list.get(index1);
Poker p2 = (Poker) list.get(index2);
list.set(index1, p2);
list.set(index2, p1);
}
}
// 有规律的显示扑克牌
public static void showPuke(LinkedList list) {
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + "\t");
if (i % 10 == 9) {
System.out.println();
}
}
}
// 生成扑克牌
public static LinkedList getPuke() {
// 集合用于存储扑克牌对象
LinkedList list = new LinkedList();
// 定义数组存储所有的花色和点数
String[] colors = { "黑桃", "红桃", "梅花", "方块" };
String[] points = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
for (int i = 0; i < colors.length; i++) {
for (int j = 0; j < points.length; j++) {
list.add(new Poker(colors[i], points[j]));
}
}
return list;
}
}
使用存储一副扑克牌,实现洗牌功能。
最新推荐文章于 2021-03-07 20:26:14 发布