import java.util.Random; /** * 扑克牌洗牌,扑克牌我设为是同一花色的从01到54 */ public class Xipai { private int size = 54; //多少张 private int[] puke; //扑克数组 //构造方法 public Xipai(){ puke = new int[size]; for (int i = 0; i < puke.length; i++) { puke[i] = i+1; } } //洗牌方法 public void xishuashua(){ Random rdm = new Random(); for (int i = size-1; i>1; i--) { int a = rdm.nextInt(i-1); this.exchange(puke, i, a); } } //交换数组元素 private void exchange(int[] array, int i,int j){ int k = array[i]; array[i] = array[j]; array[j] = k; } //打印数组 private void print(){ for (int i = 0; i < puke.length; i++) { if(puke[i]<10)System.out.print(0); System.out.print(puke[i]+" ,"); if((i+1)%10==0)System.out.println(); } System.out.println(); } public static void main(String[] args) { Xipai x = new Xipai(); System.out.println("洗牌前:"); x.print(); x.xishuashua(); System.out.println("洗牌后:"); x.print(); } }