import java.util.Random;
public class Xipai
{ public static void main (String [] args)
{
Random x=new Random(); 
int i=0,pr=0,u=0,p=0;
int num[]=new int[54];
int num2[]=new int[54];
for(i=0;i<54;i++)
{
num[i]=i+1;
System.out.print(num[i]+"\t");
}
System.out.println();
System.out.println("#################    ###############################    ################");
for(i=0;i<54;i++)
{
pr=x.nextInt(54); //在1-54里获得一个随机数字作为打乱输出数组值的下标!
if(pr==54)
pr=0; //数组的最小下标为0;用54代替0;
for(p=0;p<=i;p++)
{
if(pr==num2[p])
u=1; //判断随机数值与负责存储随机数数组里的值是否有重复。
}
if(u!=1)
{
   num2[i]=pr;//无重复就把这个随机值存进数组;
   System.out.print(num[pr]+"\t");//把这个随机值作为数组下标输出
}
else
{
                   -- i; //如果有重复的话让控制主循环的i-1重复这次迭代!
  }
}

}
}

解决方案 »

  1.   

    这是以前的帖子
    import java.util.ArrayList
    ;
    class Pai {
    char c ;  //花色
    int n ;   //面值
    Pai() {

    }
    public String toString(){
    return ""+c+n+" ";
    }
    }public class FaiPai {
    Pai[] total = new Pai[52]; //52张牌,无大小鬼
    ArrayList[] player = new ArrayList[4]; //四个玩家


    FaiPai() {

    for (int i=0;i<4;i++)
    player[i] = new ArrayList();
    //total = new Pai[52];
    for (int i=0;i<52;i++)
    total[i] = new Pai(); for(int i = 0;i<4;i++) {
    for (int j = 0;j<13;j++) {
    switch(i){
    case 0: total[i*13+j].c = 'f'; //方片
    total[i*13+j].n = j+1;break;
    case 1: total[i*13+j].c = 'c';//草花
      total[i*13+j].n = j+1;break;
    case 2: total[i*13+j].c = 'h';//黑桃
      total[i*13+j].n = j+1;break;
    case 3: total[i*13+j].c = 'r';//红桃
    total[i*13+j].n = j+1;break;
    }
    }
    }
    }
    public void send() { //发牌
    int numberPai = 52 ;
    int k = 0;
    Pai tempCard;
    while(numberPai-->0) {
    int j = (int)(Math.random()*numberPai);
    System.out.println(numberPai+": "+j);
    player[k].add(total[j]); //??????????提示这个地方空指针异常,偶看不出来
    k++;
    if (k == 4)
    k = 0;
    System.out.println(k);

    //tempCard=total[j];
    total[j] = total[numberPai];
    //total[numberPai]=tempCard;
    }

    }
    public static void main(String[] args) {
    FaiPai one = new FaiPai();
    System.out.println("开始发牌 :");
    one.send();
    for (int i=0;i<4;i++) {
    System.out.println("玩家"+i+"的牌 :");
    {
    System.out.print(one.player[i]);
    System.out.println();
    }
    }
    }
    }
      

  2.   

    public class DeckOfCards 
    {
        private Card[] card;
        private Random rand;
        private Vector<Card> deal;
        
        DeckOfCards()
        {
            rand = new Random();
            card = new Card[52];
            
            Shuffle();
            
            deal = new Vector<Card>(card.length);
            for(int i = 0; i < card.length; i ++)
            {
                deal.add(card[i]);
            }
        }
        
        public void Shuffle()
        {
            Vector<Integer> Hearts = new Vector<Integer>(13);
            Vector<Integer> Diamonds = new Vector<Integer>(13);
            Vector<Integer> Spade = new Vector<Integer>(13);
            Vector<Integer> Clubs = new Vector<Integer>(13);
            for(Integer i = 1; i <= 13; i ++)
            {
                Hearts.add(i);
                Diamonds.add(i);
                Spade.add(i);
                Clubs.add(i);
            }
            int j ;
            for(int i = 0; i < 52;)
            {
                j = rand.nextInt(4);
                switch(j)
                {
                case 0:
                    if(Hearts.isEmpty())
                    {        
                        continue;
                    }
                    else
                    {
                        int a = rand.nextInt(Hearts.size());
                        card[i] = new Card("Hearts", Hearts.elementAt(a));
                        Hearts.removeElementAt(a);
                        i ++;
                        break;
                    }
                case 1:
                    if(Diamonds.isEmpty())
                    {
                        continue;
                    }
                    else
                    {
                        int a = rand.nextInt(Diamonds.size());
                        card[i] = new Card("Diamonds", Diamonds.elementAt(a));
                        Diamonds.removeElementAt(a);
                        i ++;
                        break;
                    }
                case 2:
                    if(Spade.isEmpty())
                    {
                        continue;
                    }
                    else
                    {
                        int a = rand.nextInt(Spade.size());
                        card[i] = new Card("Spade", Spade.elementAt(a));
                        Spade.removeElementAt(a);
                        i ++;
                        break;
                    }
                case 3:
                    if(Clubs.isEmpty())
                    {
                        continue;
                    }
                    else
                    {
                        int a = rand.nextInt(Clubs.size());
                        card[i] = new Card("Clubs", Clubs.elementAt(a));
                        Clubs.removeElementAt(a);
                        i ++;
                        break;
                    }
                }
            }
            
            
        }
        
        public void PrintAll()
        {
            for(int i = 0; i < card.length; i ++)
            {
                System.out.println( (i+1) + " " + card[i]);
            }
        }
    }
    参考一下,以前写的