扑克牌的 发牌 洗牌 程序 //听老师说是 面试题- public class Poker //扑克类
- {
- private String color;
- private String value;
- public String getColor()
- {
- return this.color;
- }
- public String getValue()
- {
- return this.value;
- }
- Poker(String color,String value)
- {
- this.color=color;
- this.value=value;
- }
- public static void main(String []args)//静态方法--主方法
- {
- Poker p[]=new Poker[PokerTools.color.length*PokerTools.value.length];
- init(p);
- System.out.println("打印所有牌");
- showPoker(p);
- System.out.println();
- System.out.println("随机洗牌后:");
- xipai(p);
- showPoker(p);
- }
- public static Poker[] xipai(Poker p[])//洗牌
- {
-
- for (int i=0;i<p.length ;i++ )
- {
- 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 init(Poker p[])//初始化牌
- {
-
- int index=0; //数组下标
- for (int i=0;i<PokerTools.color.length;i++ ) //----花色
- {
- for(int j=0;j<PokerTools.value.length;j++) //----值
- {
- p[index++]=new Poker(PokerTools.color[i],PokerTools.value[j]);
- }
- }
- }
- public static void showPoker(Poker p[])//打印
- {
-
- for (int i=0;i<p.length ;i++ )
- {
- if(i%13==0) //打印一组就换行 一组13张
- {
- System.out.println();
- }
- System.out.print(p[i].getColor()+p[i].getValue()+" ");
- }
- }
- }
- class PokerTools //扑克工具类
- {
- public static String[] color={"黑桃","红桃","梅花","方块"};//静态成员
- public static String[] value={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
- }
复制代码 |