有没有办法让这个代码成为3个不同的类?

时间:2018-02-05 16:22:25

标签: java arrays class arraylist

大家,我完成了实验室的代码,但我意识到它必须有3个名为:Card,Deck和DeckOfCards的类,它最好也需要有数组列表,事情是我真的不能了解多个类以及如何从另一个类调用事物。如果有人帮忙的话会很棒的。现在我只有一个名为DeckOfCards的类,它创建了3个数组,然后将它们与循环组合在一起,对它们进行混洗并打印组合数组

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]+"]");
        }
    }

}

1 个答案:

答案 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]+"]");
    }
}