运行结果如下:
代码如下:
import java.util.Random;
//java重洗扑克牌
public class Poker {
public static void main(String[] args) {
String[] kinds = {"黑", "红", "花", "片"};
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
int[] pokers = new int[52];
Init(pokers);
System.out.println("初始化:");
show(pokers, kinds, nums);
System.out.println("洗牌后:");
// Random rand = new Random();
// System.out.println(rand.nextInt(5));
shuffle(pokers);
show(pokers, kinds, nums);
}
public static void Init(int[] pokers) {
for (int i = 0; i < pokers.length; i++) {
pokers[i] = i;
}
}
public static void show(int[] pokers, String[] kinds, String[] nums) {
for (int i = 0; i < pokers.length; i++) {
int kind = pokers[i] / 13; //取整找出什么类型
int num = pokers[i] % 13; //求余判断牌号
System.out.print(kinds[kind] + nums[num]+" ");
if ((i + 1) % 13 == 0) {
System.out.println();
}
}
}
public static void shuffle(int[] pokers) {
Random rand = new Random(); //实际取值范围0~51 后者取不到
int temp=0;
for (int i = 0; i < pokers.length; i++) {
int j=rand.nextInt(52);
if(i!=j)
{
temp = pokers[i];
pokers[i]=pokers[j];
pokers[j]=temp;
}}
}
}