递归:为什么我有错误?

时间:2015-05-09 21:39:29

标签: java recursion

    public class RandomCard {
    int num;
    int suitNum;
    String cardSuit = new String("");
    String cardNum = new String("");
    String fullCard = new String("");
    public RandomCard(){
        num= (int)(Math.random()*13)+2;
        suitNum =(int)(Math.random()*4)+1;

        if(num == 11)
            cardNum="Jack";
        else if(num== 12)
            cardNum="Queen";
        else if(num== 13)
            cardNum= "King";
        else if(num== 14)
            cardNum= "Ace";
        else 
            cardNum= ""+num;

        if(suitNum==1)
            cardSuit= "Hearts";
        else if(suitNum==2)
            cardSuit= "Spades";
        else if(suitNum==3)
            cardSuit= "Clubs";
        else
            cardSuit= "Diamonds";
    }
    public String getNum(){
        return cardNum;
    }
    public String getSuit(){
        return cardSuit;
    }
    public String toString(){
        return fullCard= cardNum+" of "+cardSuit;
    }
}
public class RandomCardDecK {
        ArrayList<RandomCard> deck = new ArrayList<RandomCard>();
        String order = new String("");
        public RandomCardDeck(){
            while (deck.size()<52){
                checkDeck(deck);
            }
        }
        public ArrayList<RandomCard> checkDeck(ArrayList<RandomCard>Deck){
            RandomCard card = new RandomCard();
            for (int i=0; i<=Deck.size();i++){
                if (card.equals(Deck.get(i))){
                    checkDeck(Deck);
                }
                else
                    Deck.add(card);

            }

    return Deck;
    }
    public String toString(){
        for (int i=0; i<=deck.size();i++){
            order += deck.get(i);
        }
    return order;

    }
}

我试图创建一个扭打的牌组,然后展示牌组。我是编程的新手,我不明白我的程序中的递归有什么问题,但却给我一个运行时错误。

1 个答案:

答案 0 :(得分:1)

编辑2:我更新了答案,您现在可以检查结果是否正常运作

编辑3:添加比较器,以便结果更清晰

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class RandomCard implements Comparator<RandomCard> {

    int num;
    int suitNum;
    String cardSuit = "";
    String cardNum = "";

    public RandomCard() {
        num = (int) (Math.random() * 13) + 2;
        suitNum = (int) (Math.random() * 4) + 1;

        if (num == 11) {
            cardNum = "Jack";
        } else if (num == 12) {
            cardNum = "Queen";
        } else if (num == 13) {
            cardNum = "King";
        } else if (num == 14) {
            cardNum = "Ace";
        } else {
            cardNum = "" + num;
        }

        if (suitNum == 1) {
            cardSuit = "Hearts";
        } else if (suitNum == 2) {
            cardSuit = "Spades";
        } else if (suitNum == 3) {
            cardSuit = "Clubs";
        } else {
            cardSuit = "Diamonds";
        }
    }

    public int getNum() {
        return num;
    }

    public int getSuitNum() {
        return suitNum;
    }

    public String getCardSuit() {
        return cardSuit;
    }

    public String getCardNum() {
        return cardNum;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        RandomCard randomCard = (RandomCard) obj;
        return this.getNum() == randomCard.getNum() && this.getSuitNum() == randomCard.getSuitNum();
    }

    @Override
    public String toString() {
        return cardNum + " of " + cardSuit;
    }

    @Override
    public int compare(RandomCard o1, RandomCard o2) {
        int c = new Integer(o1.getNum()).compareTo(o2.getNum());
        if (c == 0) {
            c = new Integer(o1.getSuitNum()).compareTo(o2.getSuitNum());
        }
        return c;
    }

    public static class RandomCardDeck {

        ArrayList<RandomCard> deck;
        String order = "";

        public RandomCardDeck() {
            deck = new ArrayList<>();
            checkDeck(new RandomCard(), 0);
            //your deck is now full with 52 cards
        }

        public ArrayList<RandomCard> getDeck() {
            return deck;
        }

        private void checkDeck(RandomCard card, int i) {
            if (deck.size() == 52) {
                return;
            }
            if (i >= deck.size()) {
                deck.add(card);
                checkDeck(new RandomCard(), 0);
            } else if (card.equals(deck.get(i))) {
                checkDeck(new RandomCard(), 0);
            } else {
                checkDeck(card, ++i);
            }
        }

        @Override
        public String toString() {
            for (int i = 0; i <= deck.size(); i++) {
                order += deck.get(i);
            }
            return order;

        }
    }

    public static void main(String[] args) {
        RandomCardDeck cardDeck = new RandomCardDeck();
        Collections.sort(cardDeck.getDeck(), new RandomCard());
        for (RandomCard randomCard : cardDeck.getDeck()) {
            System.out.println(randomCard);
        }
    }
}

结果:

run:
2 of Hearts
2 of Spades
2 of Clubs
2 of Diamonds
3 of Hearts
3 of Spades
3 of Clubs
3 of Diamonds
4 of Hearts
4 of Spades
4 of Clubs
4 of Diamonds
5 of Hearts
5 of Spades
5 of Clubs
5 of Diamonds
6 of Hearts
6 of Spades
6 of Clubs
6 of Diamonds
7 of Hearts
7 of Spades
7 of Clubs
7 of Diamonds
8 of Hearts
8 of Spades
8 of Clubs
8 of Diamonds
9 of Hearts
9 of Spades
9 of Clubs
9 of Diamonds
10 of Hearts
10 of Spades
10 of Clubs
10 of Diamonds
Jack of Hearts
Jack of Spades
Jack of Clubs
Jack of Diamonds
Queen of Hearts
Queen of Spades
Queen of Clubs
Queen of Diamonds
King of Hearts
King of Spades
King of Clubs
King of Diamonds
Ace of Hearts
Ace of Spades
Ace of Clubs
Ace of Diamonds
BUILD SUCCESSFUL (total time: 0 seconds)