早教吧作业答案频道 -->其他-->
怎么实现从一副扑克牌中随机发不重复的两组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;
}
}
我知道怎么发一组不重复的,两组就不会写了,我刚开始学,求指导!
这是我写的发一组的
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[]{});
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[]{});
看了怎么实现从一副扑克牌中随机发不...的网友还看了以下:
∫sinx/(x+i)dx从-∞到+∞.复变问题求问·对于-∞到+∞的积分,可以用∑上半平面留数* 2020-07-15 …
给出下列命题(1)实数的共轭复数一定是实数;(2)满足|z-i|+|z+i|=2的复数z点的轨迹是 2020-07-30 …
若复数z满足(1+i)z=3+i,则复数z的共轭复数在复平面内所对应的点的坐标是()A.(-2,- 2020-08-01 …
设i是虚数单位给出下列四个命题:①1+2i的共轭复数是2+i;②若a+bi=c+di则a=cb=d 2020-08-01 …
设i是虚数单位,.z表示复数z的共轭复数.若z=1-2i,则复数z+i•.z在复平面内对应的点位于 2020-08-02 …
给出下列命题:(1)实数的共轭复数一定是实数;(2)满足|z-i|+|z+i|=2的复数z在复平面 2020-08-02 …
若复数z满足(1+i)z=2+i,则复数z的共轭复数.z在复平面内对应的点位于()A.第一象限B. 2020-08-02 …
已知复数z=(1-m2)+(m2-3m+2)i,其中m∈R(I)若复数z=0,求m的值;(II)若复 2020-11-23 …
复合句Putitwhereyoufoundit.中含有哪种从句复合句Aplaneisamachine 2020-11-28 …
数学复数有关问题复数Z满足|z-(1+i)|-|z-(1-i)|=2则复平面内的对应点的轨迹为射线X 2021-01-24 …