我写了段用数组实现的洗牌的....测试了下,应该没问题了
package xipai;
import java.util.Random;
public class Xipai {
public static void main(String[] args) {
int card[]=new int[52];
int num[]=new int[52];
System.out.println("before:");
for(int i=0;i<52;i++){
num[i]=i+1;
System.out.print(num[i]+"\t");
if((i+1)%10==0) System.out.println();
}
System.out.println();
System.out.println("after:");
Random rd=new Random();
int n=51;
for(int i=0;i<52;i++){
int j=Math.abs(rd.nextInt()%(n+1));
card[i]=num[j];
num[j]=num[n];
n--;
}
for(int i=0;i<52;i++){
System.out.print(card[i]+"\t");
if((i+1)%10==0) System.out.println();
}
}
}
版主:private Vector<Card> v;
在JB2005中去掉<Card>就可以编译通过!
我找到了一个新的方法,不用两个数组就可以直接实现!
package proj;
import java.util.Random;
public class xipai {
public xipai() {
}
public static void main(String[] args) {
xipai xipai = new xipai();
int[] card=new int[52];
System.out.println("变量赋初始值:");
for(int i=0;i<52;i++){
card[i]=i+1;
System.out.print(" "+card[i]);
if((i+1)%10==0)
System.out.println();
}
System.out.println();
System.out.println("洗动之后变为:");
Random ran1 = new Random();
for (int i = 0; i < card.length; i++) {
int temp=0;
int index=ran1.nextInt(52);
temp=card[i];
card[i]=card[index];
card[index] =temp;
}
for (int i = 0; i < 52; i++) {
System.out.print("\t"+card[i]);
if((i+1)%10==0)
System.out.println();
}
}
}
[此贴子已经被作者于2006-12-2 12:42:12编辑过]