The shuffled deck will print but the original deck will not, when I use the code below I get this : [Ljava.lang.String;@677327b6 And its repeated 52 times.
public class Deck {
public static void main(String[] args) {
String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
String[] RANKS = {
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
};
// initialising deck
int n = SUITS.length * RANKS.length;
String[] deck = new String[n];
for (int i = 0; i < RANKS.length; i++) {
for (int j = 0; j < SUITS.length; j++) {
deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];
System.out.println("Original Deck Of Cards");
System.out.println(deck);
}
}
// shuffling the cards in the deck
for (int i = 0; i < n; i++) {
int r = i + (int) (Math**strong text**.random() * (n - i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
// printing shuffled deck
System.out.println("Randomly Shuffled Deck Of Cards");
for (int i = 0; i < n; i++) {
System.out.println(deck[i]);
}
}
}
change this
for (int i = 0; i < RANKS.length; i++) {
for (int j = 0; j < SUITS.length; j++) {
deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];
System.out.println("Original Deck Of Cards");
System.out.println(deck);
}
to
System.out.println("Original Deck Of Cards");
for (int i = 0; i < RANKS.length; i++) {
for (int j = 0; j < SUITS.length; j++) {
deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];
System.out.println(deck[SUITS.length * i + j]);
}
}
Hope it helps