早教吧 育儿知识 作业答案 考试题库 百科 知识分享

怎么实现从一副扑克牌中随机发不重复的两组5张牌我知道怎么发一组不重复的,两组就不会写了,我刚开始学,求指导!这是我写的发一组的packagehuan68;importjava.lang.reflect.Array;importjava.util.Arrays;

题目详情
怎么实现从一副扑克牌中随机发不重复的两组5张牌
我知道怎么发一组不重复的,两组就不会写了,我刚开始学,求指导!
这是我写的发一组的
package huan_68;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
public class Niuniu {
/**
* @param args
*/
public static void main(String[] args) {
String[] Zhuang=gen();
System.out.println(Arrays.toString(Zhuang));
}
public static String[] gen(){
String[] pool={"红桃A","红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9","红桃10","红桃J","红桃Q","红桃K","黑桃A","黑桃2","黑桃3","黑桃4","黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q","黑桃K","方片A","方片2","方片3","方片4","方片5","方片6","方片7","方片8","方片9","方片10","方片J","方片Q","方片K","红心A","红心2","红心3","红心4","红心5","红心6","红心7","红心8","红心9","红心10","红心J","红心Q","红心K"};
Random random=new Random();
boolean[] used=new boolean[pool.length];
String[] Zhuang=new String[5];
int i=0;
while(true){
int index=random.nextInt(pool.length);
if(used[index]){
continue;
}
Zhuang[i++]=pool[index];
used[index]=true;
if(i==Zhuang.length){
break;
}
}
return Zhuang;
}
}
▼优质解答
答案和解析
... 这很简单
List cards = new ArrayList(pool);
List result = new ArrayList(5);
for(int i = 0; i < 5; i++) {
int index = new Random().nextInt(cards.size());
String card = cards.get(index);
cards.remove(index);
result.add(card);
}
return result.toArray(new String[]{});