如何使用printHand(Card [] hand)方法,其中“Card”是一个单独的类的名称

时间:2014-02-05 23:43:12

标签: java eclipse

我是java的新手,我对教授给我们的任务感到不知所措,因为我并不真正理解如何使用他为作业预先指定的类和方法。

为了使用该方法,我需要在方法main中提供什么:

public static void printHand(Card[] hand)

其中Card是一个单独的类的名称?如果有人能给我这个问题的快速解释,我将非常感激。

这是Card课程,如果有任何帮助可以回答这个问题:

public class Card {

    // Constants for representing the suits
    public final static int CLUBS = 0;
    public final static int HEARTS = 1;
    public final static int SPADES = 2;
    public final static int DIAMONDS = 3;

    // Constants for representing values of
    // ace, jack, queen, and king.
    public final static int ACE = 1;
    public final static int JACK = 11;
    public final static int QUEEN = 12;
    public final static int KING = 13;

    // Final will keep them from being changed
    // after cards are constructed.
    private final int value;
    private final int suit;

    /**
     * Constructs a card with a specified suit and value.
     * 
     * @param value
     *            the value of the card. 2 through 10 are used to specify the
     *            cards with those corresponding values, and constants exist for
     *            specifying ace, jack, queen, and king
     * @param suit
     *            the suit of the card. Use one of Card.CLUBS, Card.Hearts,
     *            Card.SPADES, or Card.DIAMONDS
     */
    public Card(int value, int suit) {
        if (value < ACE || value > KING) {
            throw new IllegalArgumentException("Illegal card value: " + value);
        }
        if (suit < CLUBS || suit > DIAMONDS) {
            throw new IllegalArgumentException("Illegal card suit: " + suit);
        }

        this.value = value;
        this.suit = suit;
    }

    /**
     * Constructs a new card with the same value and suit as the original.
     * @param original the card to be copied
     */
    public Card(Card original) {
        this(original.value, original.suit);
    }

    /**
     * Gets this card's suit.
     * 
     * @return the suit of this card
     */
    public int getSuit() {
        return suit;
    }

    /**
     * Gets this card's value
     * 
     * @return the value of this card
     */
    public int getValue() {
        return value;
    }

    /**
     * Gets a letter representing the suit
     * 
     * @return a single letter, either "C", "H", "S", or "D", representing
     *         clubs, hearts, spades, and diamonds respectively
     */
    private String getSuitString() {
        return "" + "CHSD".charAt(suit);
    }

    /**
     * Gets a one- or two-character string representing the value
     * 
     * @return either "2" through "10" or "A", "J", "Q", or "K"
     */
    private String getValueString() {
        return "A 2 3 4 5 6 7 8 9 10J Q K ".substring(2 * (value - 1), 2 * value).trim();
    }

    /**
     * Returns whether two cards have the same suit and value
     * 
     * @param other
     *            the other object to be compared
     * @return true if the other object is a card with the same suit and value
     *         as this card
     */
    public boolean equals(Object other) {
        if (!(other instanceof Card))
            return false;
        if (this == other) {
            return true;
        }

        Card that = (Card) other;

        return this.suit == that.suit && this.value == that.value;
    }

    /**
     * Returns a String representation of this card, by combining its value and
     * suit (see getValueString() and getSuitString)
     * 
     * @return a 2- or 3-character representation of this card (such as "JD" for
     *         the jack of diamonds, or "10H" for the 10 of hearts
     */
    public String toString() {
        return getValueString() + getSuitString();
    }
}

2 个答案:

答案 0 :(得分:0)

您使用Card类中的toString方法返回String

的System.out.println(card.toString);

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Card[] myHand = new Card[3];

        //King of Spades
        myHand[0] = new Card(Card.KING, Card.SPADES);
        //Ace of spades
        myHand[1] = new Card(Card.ACE, Card.SPADES);
        //two of hearts
        myHand[2] = new Card(2, Card.HEARTS);

        printHand(myHand);
    }

    public static void printHand(Card[] hand) {

        System.out.println("The hand concists of the following cards");

        for(Card card : hand) {

            System.out.println(card.toString());
        }
    }

}

答案 1 :(得分:0)

首先,你必须在main方法中创建一个Cards数组(Card[]),或者任何静态方法都可以,因为在其他静态方法中只能调用静态方法。

然后,您可以将该数组卡片传递给printHand()方法,然后该方法应该打印出添加到卡片阵列中的每张卡片。确保使用Card对象的实例填充您的卡片组。

public static void printHand(Card[] hand) {
    for (Card card : hand) {
        System.out.println(card);
    }
}

public static void main(String[] args) {
    Card[] cards = {
        new Card(Card.ACE, Card.HEARTS),
        new Card(Card.KING, Card.HEARTS),
        new Card(Card.QUEEN, Card.HEARTS),
        new Card(Card.JACK, Card.HEARTS),
        new Card(Card.QUEEN, Card.SPADES)
    };

    printHand(cards);
}

输出:

AH
KH
QH
JH
QS