使用Java中的对象打印数组

时间:2016-06-26 21:30:19

标签: arrays object playing-cards

首先,我制作了一副纸牌。然后,我尝试使用我创建的方法只打印该副本卡的前七张卡片(我需要做2次)。 nextCard是一个计数器,用于跟踪我们在甲板上的位置。

这是我的班级:公共课甲板

{
    private Card[] deck;
    private int nextCard;   


    public Deck(){
        deck = new Card[53];

        nextCard = 0;

        for(int i = 0; i < 52; i++){
            deck[i] = new Card(i);
            nextCard++;                 
        }

    }//Deck 

    public Deck(Deck existingDeck){//copy

        this.deck = new Card[52];

        for(int i=0; i < 52; i++){
            this.deck[i] = new Card(existingDeck.deck[i]);

        }
        nextCard++;
    }

    public void shuffle(){

        Card crdTemp = new Card();
        Random random = new Random();
        int num;
        nextCard = 0;

        for(int i = 0; i < 52; i++){
            num = random.nextInt (51);
            crdTemp = deck[i];
            deck[i] = deck[num];
            deck[num] = crdTemp;

            nextCard++;
        }
    }

    public Card dealACard(){
        Card crd = null;
        if(nextCard > -1){
        crd = deck[nextCard];
        nextCard--;
        }
        return crd;
    }

    public String dealAHand(int handSize){
        Card crd = null;
        String cards = "";
        for(int i = 0; i < handSize; i++){
            crd = dealACard();
            cards += crd.toString();
            //cards += dealACard().toString();
        }
        return cards;
    }

    public String toString(){

        String info = "";
        for(int i = 0; i < 52; i++){
            deck[i].toString ( );
            info += deck[i];
            nextCard++;
        }
        return info;
    }

}

然后,在我的驱动程序中:

Deck bDeck = new Deck(aDeck);
        bDeck.toString();
        String[] sevenCards = new String[bDeck];

        for(int i = 0; i < 7; i++){
            System.out.println ("Copy deck: ");
            sevenCards[i] = bDeck.toString();

        }

        for(int i = 0; i < 7; i++){
            System.out.println (sevenCards[i]);
        }


    }

我假设我将整个bDeck分配给每个sevenCards数组元素,但我不知道如何以不同方式执行。我还假设有一种方法可以做到这一点,而不是尝试创建这样的新数组,但同样,我已经经历了很多不同的想法,没有任何东西已经淘汰。非常感谢某些方向,谢谢。

1 个答案:

答案 0 :(得分:0)

如果我理解你的话,你只想打印你牌组的前七张牌吗?所以你必须访问甲板卡才能这样做:

<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" password="Blue1234" database="news" doc:name="MySQL Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="localhost"  doc:name="HTTP Request Configuration" port="8081"/>
<file:connector name="file" writeToDirectory="C:\Users\Hersh\Desktop\file" autoDelete="true" streaming="true" validateConnections="true" doc:name="File" />
<flow name="parsetemplateFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/web" doc:name="HTTP"/>
    <set-payload value="#[message.inboundProperties.'http.query.params'.q]" doc:name="Set Payload" mimeType="text/html"/>
    <db:select config-ref="MySQL_Configuration" doc:name="Database">
        <db:parameterized-query><![CDATA[select txt from news.nws where sub=#[message.payload]]]></db:parameterized-query>
    </db:select>
    <response>
        <http:static-resource-handler resourceBase="C:\Users\Hersh\Desktop\attach" defaultFile="index.html" doc:name="HTTP Static Resource Handler"/>
    </response>
    <logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
    <set-payload value="#[message.payload]" doc:name="Set Payload"/>
    <response>
        <file:outbound-endpoint path="C:\Users\Hersh\Desktop\file" outputPattern="output.txt" connector-ref="file" responseTimeout="10000" doc:name="File"/>
    </response>
</flow>
</mule>

如果您的for循环中可以访问 Deck bDeck = new Deck(aDeck); bDeck.toString(); String[] sevenCards = new String[7]; for(int i = 0; i < 7; i++){ System.out.println ("Copy deck: "); sevenCards[i] = bDeck.deck[i]; } for(int i = 0; i < 7; i++){ System.out.println (sevenCards[i]); } } 并且您正确实施了Deck.deck类的toString()方法,那么这将有效。

编辑:以下是Card的最终更改:

dealACard()