不确定如何使这个牌组洗牌

时间:2015-06-10 12:21:39

标签: java arrays sorting

import java.util.*;

public class shuffleDeck
{ 
  public static int shuffleDeck (int[] deck, int theNumber) 
  {
     int [] deck2 = new int [26];
     int [] deck3 = new int [26];
     return theNumber;
  }

  public static void main(String[] args)
  {
    int [] deck = new int [52];
    for(int i=0; i<52; i++)
    {
        deck[i]=i+1;
    }
    int count;
    count=1;
    int total=1;
    shuffleDeck(deck, count);
    System.out.println();
  }
}

我一直试图让这个牌组在过去的一小时里洗牌,没有运气。我把甲板分成两半,然后打乱数组计数它洗牌的次数,然后在洗牌后打印出阵列。我根本不确定如何添加第三个将它们全部洗掉的数组。但是,如果有人知道你是否可以介意告诉我,这将是伟大的。

2 个答案:

答案 0 :(得分:1)

我不确定为什么你将甲板分成两半,但如果你不需要,那么你可以使用Collections.shuffle()

// Create an array, fill it the way you want
Integer[] deck = new Integer[]{1, 2, 3, 4};

// Shuffle the elements in the array
Collections.shuffle(Arrays.asList(deck));

答案 1 :(得分:0)

public static void shuffle(int[] deck) {
    // the random number generator used to shuffle the deck
    Random random = new Random();
    for (int i = deck.length, j, tmp; i > 1; i--) {
        // get a random position between 0 and i-1
        j = random.nextInt(i); 
        // swap the current position (i-1) with the random one ...
        tmp = deck[i - 1];
        deck[i - 1] = deck[j];
        deck[j] = tmp;
    }
}