黑马程序员技术交流社区
标题:
两种不同的扑克发牌的程序
[打印本页]
作者:
廉伟
时间:
2012-9-29 23:55
标题:
两种不同的扑克发牌的程序
class Poker
{
Poker()
{
}
Poker(String color,String number)
{
this.color=color;
this.number=number;
}
public void setColor(String color)
{
this.color=color;
}
public void setNumber(String number)
{
this.number=number;
}
public String getColor()
{
return this.color;
}
public String getNumber()
{
return this.number;
}
public static void main(String[] args)
{
Poker [] p=new Poker [PokerTools.cs.length*PokerTools.ns.length];
init(p);
printArray(p);
System.out.println("随机输入");
getPoker(p);
printArray(p);
}
public static void init(Poker [] p)
{
int index=0;
for (int i=0;i<PokerTools.cs.length ;i++ )
{
for (int j=0;j<PokerTools.ns.length;j++ )
{
p[index++]=new Poker(PokerTools.cs[i],PokerTools.ns[j]);
}
}
}
public static Poker [] getPoker(Poker [] p)
{
for (int x=0;x<p.length ;x++ )
{
int p1=(int)(Math.random()*52);
int p2=(int)(Math.random()*52);
Poker temp=p[p1];
p[p1]=p[p2];
p[p2]=temp;
}
return p;
}
public static void printArray(Poker [] p)
{
for (int x=0;x<p.length ;x++ )
{
if (x%13==0)
{
System.out.println();
}
System.out.print(p[x].getColor()+p[x].getNumber()+" ");
}
}
}
class PokerTools
{
public static String[] cs={"黑桃","红桃","梅花","方块"};//静态成员
public static String[] ns={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
}
复制代码
第二种
public class PuKeFaPai {
public static void main(String [] args){
String[] huase=new String[]{"红桃","方块","黑桃","梅花"};
String[] paihao=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int []paizhi=new int[]{2,3,4,5,6,7,8,9,10,11,12,13,14};
String[] pai=new String [52];
String[] PaiHao=new String [52];
String[] HuaSe=new String [52];
int []PaiZhi=new int[52];
int i=0,j=0;
int k=0,m=0,n=0,p=0,s=0;
int count=0;
int a;
int b=0;
String str="";
public void FaPai()
{
for(i=0;i<4;i++){
for(j=0;j<13;j++){
pai[k]=huase[i].concat(paihao[j]);
PaiZhi[s]=paizhi[j];
k++;
s++;
}
}
while(count<51){
do{
a=(int)(Math.random()*(52-count));
str=pai[a];
pai[a]=pai[51-count];
pai[51-count]=str;
b=PaiZhi[a];
PaiZhi[a]=PaiZhi[51-count];
PaiZhi[51-count]=b;
count++;
}while(count<51);
}for(i=0;i<52;i++){
if(pai[i].length()<4){
PaiHao[i]=pai[i].substring(2,3);
}else if(pai[i].length()==4){
PaiHao[i]=pai[i].substring(2,4);
}
HuaSe[i]=pai[i].substring(0, 2);
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2