使用方法和多个类的卡片组

时间:2016-04-03 20:17:56

标签: java arrays methods

我没有输出,也不知道从哪里开始。

  

设计并实现一个名为Card的类,它代表一个标准的扑克牌。每张卡片都有西装和面值。然后,创建一个驱动程序类,将Card类的52个对象存储到一个数组中。包括洗牌的方法,交易卡片并报告卡片中留下的卡片数量。洗牌方法应该采用完整的牌组。你的主要方法应该从洗牌的套牌中处理每张牌,打印每张牌(套装和面值)。

这是我到目前为止所拥有的:

    import java.util.Random;
public class card {
public static void main(String[] args) {}

    public class deck {


        int[] deck = new int[52];
        String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

        public void create() {//initialize cards
            for (int i = 0; i < deck.length; i++) deck[i] = i;
        }


        public void shuffle() {//deck shuffle
            for (int i = 0; i < deck.length; i++) {

                int index = (int) (Math.random() * deck.length);
                int temp = deck[i];
                deck[i] = deck[index];
                deck[index] = temp;
            }



                 //display all the cards!
                for (int i = 0; i < deck.length; i++)

                {
                    String suit = suits[deck[i] / 13];
                    String rank = ranks[deck[i] % 13];
                    System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);


                }


            }
        }

        }

2 个答案:

答案 0 :(得分:3)

在java中,方法main()是代码开始运行的地方。您现在的主要方法中没有任何内容:

public static void main(String[] args) {}

要让它做某事,请将其更改为:

public static void main(String[] args) {
 deck myDeck = new deck();
 myDeck.create();
 myDeck.shuffle();
}

作为一个注释,在java中使用大写字母命名您的类是很好的做法。

答案 1 :(得分:0)

在这里,我尝试为你编写一些代码,它不完整,但提供了一个框架供你继续,你所要做的就是实现你自己的shuffle方法并为rank添加更多变量:

public class CardDriver {
    public static void main(String[] args) {

        Deck myDeck = new Deck();
        myDeck.shuffle();
        System.out.println("Dealt cards are:");
        myDeck.dealAllCards();
    }
}

class Card {
    String suite, faceValue;
    Card(String suite, String faceValue) {
        this.suite = suite;
        this.faceValue = faceValue;
    }
    void printCard() {
        System.out.println(faceValue + " " + suite);
    }
}

class Deck {
    String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
    String[] ranks = { "6", "7" };
    int count = ranks.length * suits.length;
    Card deck[] = new Card[count];

    Deck() { // fills out the deck
        int index = 0; // this is for simple deck filling
        for (int i = 0; i < ranks.length; i++) { // for ranks
            for (int j = 0; j < suits.length; j++) { // for suits
                deck[index] = new Card(suits[j], ranks[i]);
                index++;
            }
        }
    };

    void shuffle() { // shuffles the deck
        // your code here
    }

    Card dealCard() { // gives card from deck
        if (returnLeft() > 0) {
            count--;
            return deck[count];
        } else return null;
    }

    int returnLeft() {
        return count;
    }

    void dealAllCards() {
        int counter = count;
        for (int i = 0; i < counter; i++) {
            Card someCard = dealCard();
            someCard.printCard();
        }
    }
}