如何在单独的方法中打印数组

时间:2014-04-28 20:19:32

标签: java methods

我必须制作一张牌,另一类牌。我的甲板课需要我的卡类。在我的甲板课上,我必须使用与main相同的方法构建甲板,我做了。然后我还必须以与main相同的方法打印卡座。这是我的问题:我尝试使用典型的toString方法,它在main中工作正常。但我在使用自己的方法打印时却遇到了麻烦。谁能告诉我在我的打印方法中我做错了什么?任何帮助表示赞赏!

卡类:

public class card
{
    private int face;
    private String suit;

    public card()
    {
        face = 0;
        String suit = " ";
    }

    public card(int face, String suit)
    {
        setFace(face);
        setSuit(suit);

    }

    public void setFace(int f)
    {
        if(f >= 1 && f <= 10)
            face = f;

        else
            face = 1;
    }

    public int getFace()
    {
        return face;
    }

    public void setSuit(String s)
    {
        if( s == "Hearts" && s == "Clubs" && s == "Diamonds" && s ==  "Ace")
            suit = s;
        else
            suit = "Hearts";
    }

    public String getColor()
    {
        return suit;
    }

}

甲板课程:

import java.util.*;

public class DeckOfCards
{
    public static void main(String[] arg)
    {
        card myCard = new card();

        card deck[] = new card[52];

        //System.out.println(Arrays.toString(deck));
        buildDeck(deck);

        System.out.print(myCard);

    }

    // method builds deck
    public static void buildDeck(card[] cardList)
    {
        cardList[0] = new card(1, "Hearts");
        cardList[1] = new card(2, "Hearts");
        cardList[2] = new card(3, "Hearts");
        cardList[3] = new card(4, "Hearts");
        cardList[4] = new card(5, "Hearts");
        cardList[5] = new card(6, "Hearts");
        cardList[6] = new card(7, "Hearts");
        cardList[7] = new card(8, "Hearts");
        cardList[8] = new card(9, "Hearts");
        cardList[9] = new card(10, "Hearts");
        cardList[10] = new card(11, "Hearts");
        cardList[11] = new card(12, "Hearts");
        cardList[12] = new card(13, "Hearts");

        cardList[13] = new card(1, "Clubs");
        cardList[14] = new card(2, "Clubs");
        cardList[15] = new card(3, "Clubs");
        cardList[16] = new card(4, "Clubs");
        cardList[17] = new card(5, "Clubs");
        cardList[18] = new card(6, "Clubs");
        cardList[19] = new card(7, "Clubs");
        cardList[20] = new card(8, "Clubs");
        cardList[21] = new card(9, "Clubs");
        cardList[22] = new card(10, "Clubs");
        cardList[23] = new card(11, "Clubs");
        cardList[24] = new card(12, "Clubs");
        cardList[25] = new card(13, "Clubs");

        cardList[26] = new card(1, "Diamonds");
        cardList[27] = new card(2, "Diamonds");
        cardList[28] = new card(3, "Diamonds");
        cardList[29] = new card(4, "Diamonds");
        cardList[30] = new card(5, "Diamonds");
        cardList[31] = new card(6, "Diamonds");
        cardList[32] = new card(7, "Diamonds");
        cardList[33] = new card(8, "Diamonds");
        cardList[34] = new card(9, "Diamonds");
        cardList[35] = new card(10, "Diamonds");
        cardList[36] = new card(11, "Diamonds");
        cardList[37] = new card(12, "Diamonds");
        cardList[38] = new card(13, "Diamonds");

        cardList[39] = new card(1, "Diamonds");
        cardList[40] = new card(2, "Diamonds");
        cardList[41] = new card(3, "Diamonds");
        cardList[42] = new card(4, "Diamonds");
        cardList[43] = new card(5, "Diamonds");
        cardList[44] = new card(6, "Diamonds");
        cardList[45] = new card(7, "Diamonds");
        cardList[46] = new card(8, "Diamonds");
        cardList[47] = new card(9, "Diamonds");
        cardList[48] = new card(10, "Diamonds");
        cardList[49] = new card(11, "Diamonds");
        cardList[50] = new card(12, "Diamonds");
        cardList[51] = new card(13, "Diamonds");

    }

    // method to print deck
    public String toString()
    {
        System.out.println(Arrays.toString(deck));
    }

} 

3 个答案:

答案 0 :(得分:1)

你的toString方法应该返回一个String,而不是打印它。

所以,替换这个:

public String toString(){
     System.out.println(Arrays.toString(deck));
}

通过以下方式:

public String toString(){
     return Arrays.toString(deck);
}

答案 1 :(得分:1)

您的deck[]被定义为main方法中的局部变量。您需要将其移出并使其成为静态类级变量。

此外,您可能不应该调用您的打印方法toString()toStringObject上的现有方法,您正在此处进行投影。

答案 2 :(得分:0)

我个人会做这样的事情:

public class Cards {

    public static void main(String[] args) {

        DeckOfCards deck = new DeckOfCards();
        System.out.println( deck );
    }
}


public class CardSuit {

    public static final CardSuit CLUBS = new CardSuit("Clubs");
    public static final CardSuit DIAMONDS = new CardSuit("Diamonds");
    public static final CardSuit HEARTS = new CardSuit("Hearts");
    public static final CardSuit SPADES = new CardSuit("Spades");

    private String suit;

    private CardSuit( String suit ){
        this.suit = suit;
    }

    @Override public String toString(){ return this.suit; }
}

public class CardFace {

    private String face;

    public CardFace( int value ){

        this.face = this.convertValueToFace( value );
    }

    private String convertValueToFace( int value ){

        if ( value > 1 && value < 11 ){
            return Integer.toString( value );
        }

        switch( value ){
            case 11 : { return "J"; }
            case 12 : { return "Q"; }
            case 13 : { return "K"; }
            case 1 : { return "A"; }
        }

        throw new IllegalArgumentException( 
            "Card face value must be between 1 and 13"
        );
    }

    @Override public String toString(){ return this.face; }
}

public class Card {

    private CardSuit suit;
    private CardFace face;

    public Card( CardSuit suit, CardFace face ){
        this.suit = suit;
        this.face = face;
    }

    @Override public String toString(){ 
        return this.face + " of " + this.suit; 
    }
}

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DeckOfCards {

private List<Card> cards;

    public DeckOfCards(){
        this.createDeck();
    }

    private void createDeck(){

        this.cards = new ArrayList<Card>();

        List<CardSuit> suits = new ArrayList<CardSuit>();
        suits.add( CardSuit.HEARTS );
        suits.add( CardSuit.DIAMONDS );
        suits.add( CardSuit.CLUBS );
        suits.add( CardSuit.SPADES );

        List<CardFace> faces = new ArrayList<CardFace>();

        int valueStart = 1;
        int valueEnd = 13;

        for ( int value = valueStart; value <= valueEnd; value++ ){
            faces.add( new CardFace( value ) );
        }

        Iterator<CardSuit> suitIterator = suits.iterator();
        while( suitIterator.hasNext() ){

            CardSuit suit = suitIterator.next();

            Iterator<CardFace> faceIterator = faces.iterator();
            while( faceIterator.hasNext() ){
                Card card = new Card( suit, faceIterator.next() );
                this.cards.add( card );
            }
        }
    }

    @Override public String toString(){
        String result = "Card Deck: [\n";
        Iterator<Card> cardIterator = this.cards.iterator();
        while( cardIterator.hasNext() ){
            result += "\t" + cardIterator.next().toString() + "\n";
        }
        result += "]";
        return result;
    }

}