import java.util.*;
public class t发牌器 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Card bt=new Card("黑桃");
System.out.println("how many cards you want?(<13)");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
bt.distribute(n);
}
}
class Card{
String number[]= {"null","A","J","Q","K","2","3","4","5","6","7","8"
,"9","10"};//number[0]为空;
String c;
public Card(String c) {//构造函数,定义牌的种类
this.c=c;
}
public void distribute(int n) {
Random rand=new Random();
int index[]=new int[n];//用于存入number数组的下标,默认值为0
int count=0;//index数组的下标
int flag=1;//判断是否重复,重复为0
while(true){
int r=rand.nextInt(13)+1;//获得1-14间的随机整数
for(int i=0;i<n;i++){//该循环判断是否重复,重复则跳出循环并执行下面的代码
if(r==index[i]) {
flag=0;break;
}
}
if(flag==1) {
index[count]=r;//将随机数覆盖index数组
count++;
}
if(index[n-1]!=0) {//index最后一个被覆盖时,结束循环
break;
}
}
for(int i=0;i<n;i++) {
System.out.print(c+number[index[i]]+" ");
}
}
}
给定的n比较大时,程序便一直处于运行中,不出结果;
希望能在原来的基础上改良代码;