嘿,这么巧,我昨天刚做了个炸金花小程序,我感觉最关键还是现实中抽一张牌就不会再抽出来了,但是计算机会出现重复的牌,需要判断再抽牌,若是重复就让他继续随机抽。下面是我的小程序:欢迎交流指正!
import java.util.Random;
class PukeArray1
{
public static void main(String[] args) {
//为了保证不重复并且好判断,就使用二维数组,方法比较笨点
String[][] puke = {{"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"}};
for(int x=0;x<10;x++){
Random ra = new Random();
int hua1 = ra.nextInt(4);
int shu1 = ra.nextInt(13); //此处,一维数组的索引应该由0-12;
int hua2 = ra.nextInt(4);
int shu2 = ra.nextInt(13);
int hua3 = ra.nextInt(4);
int shu3 = ra.nextInt(13);
while((hua1*100+shu1)==(hua2*100+shu2)|(hua1*100+shu1)==(hua3*100+shu3)| (hua2*100+shu2)==(hua3*100+shu3))//判断有没有完全相同的牌。用"|"判断,有一个相等就不行,因为
{ //现在乘积都完全保证不会有重复,一旦有一个重复,一定是完全一样。
hua1 = ra.nextInt(4);
shu1 = ra.nextInt(13);
hua2 = ra.nextInt(4);
shu2 = ra.nextInt(13);
hua3 = ra.nextInt(4);
shu3 = ra.nextInt(13);
}
getType(hua1);
System.out.print(puke[hua1][shu1]+" ");
getType(hua2);
System.out.print(puke[hua2][shu2]+" ");
getType(hua3);
System.out.print(puke[hua3][shu3]+" ");
System.out.println();
}
}
public static void getType(int x)//随机花色。
{
switch(x)
{
case 0:
System.out.print("红桃");
break;
case 1:
System.out.print("方块");
break;
case 2:
System.out.print("梅花");
break;
case 3:
System.out.print("黑桃");
break;
default:
System.out.println("错误花色");
break;
}
}
}
|