{}
run-icon
Main.java
import java.io.PrintStream; import java.util.Random; import java.util.random.RandomGenerator; class Main implements Runnable { public enum Suit { HEARTS, DIAMONDS, SPADES, CLUBS; } public enum Value { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING; } public enum Card { S1(Suit.SPADES, Value.ACE), S2(Suit.SPADES, Value.TWO), S3(Suit.SPADES, Value.THREE), S4(Suit.SPADES, Value.FOUR), S5(Suit.SPADES, Value.FIVE), S6(Suit.SPADES, Value.SIX), S7(Suit.SPADES, Value.SEVEN), S8(Suit.SPADES, Value.EIGHT), S9(Suit.SPADES, Value.NINE), S10(Suit.SPADES, Value.TEN), SJ(Suit.SPADES, Value.JACK), SQ(Suit.SPADES, Value.QUEEN), SK(Suit.SPADES, Value.KING), ; private final Suit suit; private final Value value; private Card(Suit suit, Value value) { this.suit = suit; this.value = value; } public Suit getSuit() { return suit; } public Value getValue() { return value; } public String toString() { return getValue() + "_" + getSuit(); } } private final PrintStream out; private final Card[] deck; private final RandomGenerator rand; public Main(PrintStream out, RandomGenerator rand) { this.out = out; this.deck = Card.values(); this.rand = rand; } public void run() { out(deck); shuffle3(); outln(); out(deck); } private void shuffle() { for(int i = deck.length - 1; i >= 0; i--) { int j = (int) (Math.random()*(i+1)); Card k = deck[i]; deck[i] = deck[j]; deck[j] = k; } } private void shuffle2() { for(int i = 0; i < deck.length; i++) { int j = (int) (Math.random()*(i+1)); Card k = deck[i]; deck[i] = deck[j]; deck[j] = k; } } private void shuffle3() { for(int i = 0; i < deck.length; i++) { int j = (int) (rand.nextInt(deck.length)); Card k = deck[i]; deck[i] = deck[j]; deck[j] = k; } } private void out(Card[] deck) { for (int i = 0; i < deck.length; i++) { out(String.valueOf(deck[i])); } } private void out(String message) { out.println(message); } private void outln() { out.println(); } public static void main(String[] args) { new Main(System.out, new Random(2L)).run(); } }
Output