feat(class14): 真的,不能再....学了.....

- 新增 ArrayTest4 类,包含 main 方法作为程序入口
- 实现 start 方法,创建并初始化一副扑克牌数组
- 实现 shufflePokers 方法,使用 Fisher-Yates 算法对扑克牌进行洗牌
- 在 main 方法中调用 start 和 shufflePokers 方法,展示洗牌前后的扑克牌数组
This commit is contained in:
2025-07-07 02:12:12 +08:00
parent 128ce511a3
commit 206c73e8ac

View File

@@ -0,0 +1,40 @@
package class14;
import java.util.Arrays;
// ... existing code ...
public class ArrayTest4 {
public static void main(String[] args) {
String[] pokers = start();
String[] shuffledPokers = shufflePokers(pokers);
System.out.println(Arrays.toString(shuffledPokers));
}
public static String[] start() { // 创建一个数组,用来模拟一个扑克牌
String[] pokers = new String[54];
String[] colors = {"", "", "", ""};
String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
int i = 0;
for (String number : numbers) {
for (String color : colors) {
pokers[i] = color + number;
i++;
}
}
pokers[i] = "小王";
pokers[i + 1] = "大王";
System.out.println(Arrays.toString(pokers));
return pokers;
}
public static String[] shufflePokers(String[] pokers) {
// 使用 Fisher-Yates 洗牌算法
for (int i = pokers.length - 1; i > 0; i--) {
int index = (int) (Math.random() * (i + 1));
String temp = pokers[i];
pokers[i] = pokers[index];
pokers[index] = temp;
}
return pokers;
}
}