public class DeckOfCards{
public static void main(String[] args) {
String[] s = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
String[] face = {
"1" ,"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
};
String[] ranks = {"1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "10", "10","10", "1 or 11"
};
int n = s.length * face.length;
String[] deck = new String[n];
for (int i = 0; i < face.length; i++) {
for (int j = 0; j < s.length; j++) {
deck[s.length*i+j] = s[j] + ", " + face[i] + ", " + ranks[i];
}
}
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n-i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
for (int i = 0; i < n; i++) {
System.out.println("["+deck[i]+"]");
}
}
}
答案 0 :(得分:2)
你可能正在寻找这样的东西:
enum Suit {
Clubs,
Diamonds,
Hearts,
Spades;
}
enum Face {
Ace(1,11),
Two(2),
Three(3),
Four(4),
Five(5),
Six(6),
Seven(7),
Eight(8),
Nine(9),
Ten(10),
Jack(10),
Queen(11),
King(12);
private final int rank;
private final int alternativeRank;
private Face(int rank, int alternativeRank) {
this.rank = rank;
this.alternativeRank = alternativeRank;
}
private Face(int rank) {
// Default to 0 for alternative rank.
this(rank,0);
}
public int getRank() {
return rank;
}
public int getAlternativeRank() {
return alternativeRank;
}
}
class Card {
private final Suit suit;
private final Face face;
public Card(Suit suit, Face face) {
this.suit = suit;
this.face = face;
}
public Suit getSuit() {
return suit;
}
public Face getFace() {
return face;
}
@Override
public String toString() {
return "Card{" +
"suit=" + suit +
", face=" + face +
'}';
}
}
public void test(String[] args) {
// This should look familliar.
Suit[] suits = Suit.values();
Face[] faces = Face.values();
Card[] deck = new Card[suits.length * faces.length];
int c = 0;
for ( Suit s : Suit.values()) {
for (Face f : Face.values()) {
deck[c++] = new Card(s,f);
}
}
for (int i = 0; i < deck.length; i++) {
int r = i + (int) (Math.random() * (deck.length-i));
Card temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
for (int i = 0; i < deck.length; i++) {
System.out.println("["+deck[i]+"]");
}
}