Java Array select random cards from deck

Question

The problem is to create a program that will randomly select four cards from a deck of 52 cards.

All the cards are stored using an array named deck, filled with initial values 0 to 51, as follows:

int[] deck = new int[52]; 

// Initialize cards 
for (int i = 0; i < deck.length; i++) 
   deck[i] = i; 

Card numbers 0 to 12, 13 to 25, 26 to 38, and 39 to 51 represent 13 Spades, 13 Hearts, 13?Diamonds, and 13 Clubs, respectively.

cardNumber / 13 determines the suit of the card and cardNumber % 13 determines the rank of the card.

After shuffling the array deck, pick the first four cards from deck.

public class Main {
  public static void main(String[] args) {
    int[] deck = new int[52];
    String[] suits = {"Spades", "Hearts", "Clubs", "Diamonds"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
      "10", "Jack", "Queen", "King"};
        //from  ww w . ja  va2 s. co m
    //your code
    
  }
}



public class Main {
  public static void main(String[] args) {
    int[] deck = new int[52];
    String[] suits = {"Spades", "Hearts", "Clubs", "Diamonds"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
      "10", "Jack", "Queen", "King"};
        
    // Initialize cards
    for (int i = 0; i < deck.length; i++)
      deck[i] = i;
    
    // Shuffle the cards
    for (int i = 0; i < deck.length; i++) {
      // Generate an index randomly
      int index = (int)(Math.random() * deck.length);
      int temp = deck[i];
      deck[i] = deck[index]; 
      deck[index] = temp;
    }

    // Display the first four cards
    for (int i = 0; i < 4; i++) {
      String suit = suits[deck[i] / 13];
      String rank = ranks[deck[i] % 13];
      System.out.println("Card number " + deck[i] + ": " 
        + rank + " of " + suit);
    }
  }
}



PreviousNext

Related