计算52张扑克牌中的四张牌
代码如下:
package ClassDemo;
public class DeckOfCards {
public static void main (String[] args) {
int[] deck = new int[52];
for (int i = 0; i < deck.length; i++) {
deck[i] = i;
}
shuffleDeck(deck);
showFourCards(deck);
}
/**
* Display the first four cards
* @param deck
*/
private static void showFourCards(int[] deck) {
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
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]);
System.out.print(suit + " ");
System.out.println(rank);
}
}
private static void shuffleDeck(int[] deckToShuffle) {
// 7.shuffling
for (int i = 0; i < deckToShuffle.length; i++) {
int index = (int) (Math.random() * deckToShuffle.length);
// myList[index] myList[i]
int tempVar = deckToShuffle[i];
deckToShuffle[i] = deckToShuffle[index];
deckToShuffle[index] = tempVar;
}
}
}