Java 我的交易方法行不通。请帮忙?

Java 我的交易方法行不通。请帮忙?,java,arraylist,Java,Arraylist,所以我正在制作一个纸牌游戏,并且一直在为我的计算机科学课程使用一个模板。但是,没有应答键来检查并确保我所做的是正确的。所以问题是,我试图使用arraylist和交易卡,但我不确定如何解决交易。我是Java新手,因此非常感谢您的帮助。下面是我的代码: public class Deck { /** * cards contains all the cards in the deck. */ private List<Card> cards;

所以我正在制作一个纸牌游戏,并且一直在为我的计算机科学课程使用一个模板。但是,没有应答键来检查并确保我所做的是正确的。所以问题是,我试图使用arraylist和交易卡,但我不确定如何解决交易。我是Java新手,因此非常感谢您的帮助。下面是我的代码:

public class Deck {

    /**
     * cards contains all the cards in the deck.
     */
    private List<Card> cards;

    /**
     * size is the number of not-yet-dealt cards.
     * Cards are dealt from the top (highest index) down.
     * The next card to be dealt is at size - 1.
     */
    private int size;



    /**
     * Creates a new <code>Deck</code> instance.<BR>
     * It pairs each element of ranks with each element of suits,
     * and produces one of the corresponding card.
     * @param ranks is an array containing all of the card ranks.
     * @param suits is an array containing all of the card suits.
     * @param values is an array containing all of the card point values.
     */
    public Deck(String[] ranks, String[] suits, int[] values) {
        ArrayList<Card> deck = new ArrayList<Card>();
        for(int a = 0; a<=ranks.length; a++){
            for(int b=0; b<=suits.length;b++){
                for(int c=0; c<=values.length; c++){
                    deck.add(new Card(ranks[a],suits[b],c));
                }
            }

        }
    }


    /**
     * Determines if this deck is empty (no undealt cards).
     * @return true if this deck is empty, false otherwise.
     */
    public boolean isEmpty() {
        if(size==0){
            return true;
        }
        else{
            return false;
        }
    }

    /**
     * Accesses the number of undealt cards in this deck.
     * @return the number of undealt cards in this deck.
     */
    public int size() {
     return size;
    }
    /**
     * Randomly permute the given collection of cards
     * and reset the size to represent the entire deck.
     */
    public void shuffle() {
        /* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
    }

    /**
     * Deals a card from this deck.
     * @return the card just dealt, or null if all the cards have been
     *         previously dealt.
     */
    public Card deal() {
        while(size>0){
            for(int i = 0;i<=size;i++){
                return deck[i];//This cannot be resolved to the deck arraylist above
                cards.remove(0);

            }
        }
    }

    /**
     * Generates and returns a string representation of this deck.
     * @return a string representation of this deck.
     */
    @Override
    public String toString() {
        String rtn = "size = " + size + "\nUndealt cards: \n";

        for (int k = size - 1; k >= 0; k--) {
            rtn = rtn + cards.get(k);
            if (k != 0) {
                rtn = rtn + ", ";
            }
            if ((size - k) % 2 == 0) {
                // Insert carriage returns so entire deck is visible on console.
                rtn = rtn + "\n";
            }
        }

        rtn = rtn + "\nDealt cards: \n";
        for (int k = cards.size() - 1; k >= size; k--) {
            rtn = rtn + cards.get(k);
            if (k != size) {
                rtn = rtn + ", ";
            }
            if ((k - cards.size()) % 2 == 0) {
                // Insert carriage returns so entire deck is visible on console.
                rtn = rtn + "\n";
            }
        }

        rtn = rtn + "\n";
        return rtn;
    }
}

尽管我尚未完全理解您的问题,但这可能会帮助您:

我发现的一个问题是ArrayList数据组的可见性:


缺少语言标记。您还需要花时间调试它并确定问题可能出在哪里。
public Deck(String[] ranks, String[] suits, int[] values) {
    ArrayList<Card> deck = new ArrayList<Card>();
    ...
}
public class Deck {

    private List<Card> cards;
    private int size;
    private List<Card> deck;

    public Deck(String[] ranks, String[] suits, int[] values) {
        deck = new ArrayList<Card>();
        for(int a = 0; a<=ranks.length; a++) {
            for(int b=0; b<=suits.length;b++) {
                for(int c=0; c<=values.length; c++) {
                    deck.add(new Card(ranks[a],suits[b],c));
                }
            }
        }
    }