Reputation: 17
Is it possible to use hashtags in java? Does tho hash tag serve any purpose?
Upvotes: 0
Views: 1173
Reputation: 14800
The for
loop within main()
in your posted code builds the deck from the two string arrays and prints it out as it's building it, but after building the deck you are never actually shuffling it.
After your initial for loop you have to call ShuffledDeck
(which should be called shuffleDeck
then loop over the array again printing each card. Basically:
for (int i = 0; i<number.length; i++) {
// ...building the deck as before...
}
shuffleDeck(deck);
for (int i = 0; i<deck.length; i++) {
System.out.println(deck[i]);
}
Upvotes: 0
Reputation: 4048
As mentioned in my comment, you could simplify your shuffledDeck()
method:
public static void shuffledDeck(String[] deck) {
List<String> shuffledDeck = new ArrayList(Arrays.asList(deck));
Collections.shuffle(shuffledDeck);
for (String card : shuffledDeck){
System.out.println(card);
}
}
Upvotes: 0
Reputation: 1599
for (k = 52; k<deck.length; k--){
this loop is bad built.
If you start in k = 52, you want to loop until k is 1 (since there is no need to swap the last element with himself). This way, k wont be < than deck.lenght and the body of the loop will never execute.
Change it to
for (k = deck.length; k >= 1; k--){
Then, when you make this one:
for (l = 0; l<k; l++){
k will be 0, so this loop will have the same problem, so change it to
for (l = 0; l<deck.length; l++){
Going a bit further, the Fisher-Yates shuffle swaps each element with one of the remaining elements on the collection, so
int index = shuffle.nextInt(k-1);
would be more correct
Upvotes: 1
Reputation: 969
what you want in your for loop is
for(k = deck.length - 1; k >= 0; k--)
your indexing is off and it never enters the shuffle algorithm ( I didn't check the correctness of it FYI so you might bump into other problems)
Upvotes: 0