想产生三张牌,可是有时候这个产生的随机数会导致产生两个一模一样的牌,如红桃A,红桃A,红桃3;不能有两个红桃A,怎么改?
public class jinhua {
private int value;
private int huase;
private String strhuase;
public void setValue()
{
this.value=(int)(Math.random()*13)+1;
this.huase=(int)(Math.random()*4)+1;
if(this.huase==1)
this.strhuase="黑桃";
if(this.huase==2)
this.strhuase="红桃";
if(this.huase==3)
this.strhuase="梅花";
if(this.huase==4)
this.strhuase="方块";}
public void showValue()
{
for(int i=0;i<3;i++)
{
this.setValue();
if(this.value==1)
{
System.out.print(this.strhuase+"A");
continue;
}
if(this.value==11)
{
System.out.print(this.strhuase+"J");
continue;
}
if(this.value==12)
{
System.out.print(this.strhuase+"Q");
continue;
}
if(this.value==13)
{
System.out.print(this.strhuase+"K");
continue;
}
else
System.out.print(this.strhuase+this.value);
}
}
public static void main(String[] args)
{
jinhua yinzai=new jinhua();
yinzai.setValue();
yinzai.showValue();
}
}
public class jinhua {
private int value;
private int huase;
private String strhuase;
public void setValue()
{
this.value=(int)(Math.random()*13)+1;
this.huase=(int)(Math.random()*4)+1;
if(this.huase==1)
this.strhuase="黑桃";
if(this.huase==2)
this.strhuase="红桃";
if(this.huase==3)
this.strhuase="梅花";
if(this.huase==4)
this.strhuase="方块";}
public void showValue()
{
for(int i=0;i<3;i++)
{
this.setValue();
if(this.value==1)
{
System.out.print(this.strhuase+"A");
continue;
}
if(this.value==11)
{
System.out.print(this.strhuase+"J");
continue;
}
if(this.value==12)
{
System.out.print(this.strhuase+"Q");
continue;
}
if(this.value==13)
{
System.out.print(this.strhuase+"K");
continue;
}
else
System.out.print(this.strhuase+this.value);
}
}
public static void main(String[] args)
{
jinhua yinzai=new jinhua();
yinzai.setValue();
yinzai.showValue();
}
}
boolean[] boo = new boolean[54];
这样54个数都是false
1:之后把你生成的数
对应的在boo[num] = true;
赋值为ture 下次再产出随机数的时候
就到boo数组里面检测下
这个随机数对应的boo里面是true 还是 false
要是true 就说明已经有了 得重新产生
要是false 就说明没有 同时把这个随机数 对应的boo类型数组 复制false2:还有你可以定义一个数组,数组里面有方法可以让数变成无序的。
那么再从头便利出来你想要的几个就可以了。不用随机产生
3:你可以把组合好的扑克牌放进set集合中,因为set集合是无序不可重复的。
放进去之后检查下这个集合的size 要是你要的数量就说明没有重复的
要是少了,那么再往里面放。直到你想要的数量
获取洗完牌的前3张import java.util.Random;/*
* 想产生三张牌,可是有时候这个产生的随机数会导致产生两个一模一样的牌,
* 如红桃A,红桃A,红桃3;不能有两个红桃A,怎么改?
*
* 用随机数洗牌而不是产生牌
* 获取洗完牌的前3张
*/
public class Test { public static void main(String[] args) {
//获取洗好的扑克牌
String[] pukes = Puke.getRandomPuke();
//获取前3张牌
final int count = 3;
System.out.println("随机获取3张扑克牌");
for(int i = 0;i < count;i++){
System.out.print(pukes[i] + ",");
}
System.out.println();
}}class Puke{
//扑克牌
private static String[] pukes;
//花色
private final static String[] flowers = new String[]{"红桃","方片","梅花","黑桃"};
//大小
private final static String[] values = new String[]{"A","2","3","4","5","6","7","8","9","10",
"J","Q","K"
};
/*随机产生一副牌*/
public static String[] getRandomPuke(){
final int total = 52;
if(pukes == null){
pukes = new String[total];
for(int i = 0;i < flowers.length;i++){
for(int j = 0;j < values.length;j++){
pukes[values.length * i + j] = flowers[i] + values[j];
}
}
}
//洗牌
Random r = new Random();
for(int i = 0;i < pukes.length;i++){
int index = r.nextInt(total);
String temp = pukes[index];
pukes[index] = pukes[i];
pukes[i] = temp;
}
return pukes;
}}
将n张牌放入一个集合A.发的牌放入另一个集合B.首先从A 随机取一张牌,放入到集合B.同时从A中移除此牌
然后如此循环处理,发再多的牌也不会重复.定义牌的类,重写equals方法,花色,大小相同才equals,然后重写hashCode方法...
private int huase;
private String strhuase;
private int temp;
public void setValue() {
this.huase = (int) (Math.random() * 4) + 1;
this.value = (int) (Math.random() * 13) + 1;
if (this.huase == 1)
this.strhuase = "黑桃";
if (this.huase == 2)
this.strhuase = "红桃";
if (this.huase == 3)
this.strhuase = "梅花";
if (this.huase == 4)
this.strhuase = "方块";
}
public void showValue() {
this.setValue();
temp = this.huase;
for (int i = 0; i < 3; i++) {
if(i==0){
getValue();
continue;
}else{
this.setValue();
if(temp == this.huase){
this.setValue();
}else{
getValue();
}
}
}
}
public void getValue(){
if (this.value == 1) {
System.out.print(this.strhuase + "A");
}else if (this.value == 11) {
System.out.print(this.strhuase + "J");
}else if (this.value == 12) {
System.out.print(this.strhuase + "Q");
}else if (this.value == 13) {
System.out.print(this.strhuase + "K");
} else
System.out.print(this.strhuase + this.value);
}
public static void main(String[] args) {
jinhua yinzai = new jinhua();
yinzai.showValue();
}// 写的不是很好,坐等高人指点