标题:发扑克牌的程序,不知道应该怎么发牌啊
取消只看楼主
vdestroyer
Rank: 2
等 级:论坛游民
帖 子:136
专家分:14
注 册:2009-1-7
结帖率:96.43%
 问题点数:0 回复次数:0 
发扑克牌的程序,不知道应该怎么发牌啊
这个练习的要求就是写出一个程序,发出5张扑克牌,并且打印出这5张牌是什么。

这个练习要求:
Class Card 里的东西不能被改,只能改Deck的代码,CardMain的代码要自己写。
Deck的getCard,fill和toString我都不知道该怎么写。
getCard里面我写了一些,也不知道这么写对不对。
fill和toString都不知道该怎么写。
Deck里面的toString必须要呼叫Card里面的toString才行。

我写的这个程序打印出来就变成:
The five card you have got are: Jack of spades
The five card you have got are: Jack of spades
The five card you have got are: Jack of spades
The five card you have got are: Jack of spades
The five card you have got are: Jack of spades

求哪位高手能帮帮我啊,告诉我些具体的思路就行。

下面是代码:
程序代码:
package lab02;

public class CardMain
{
    public static void main(String[] args)
    {
        Deck d = new Deck();
        Card c = new Card(11, 3);
        
        d.getCard();
        
        for( int i = 0; i < 5; i++)
        {
            System.out.println("The five card you have got are: "
                    + c.toString());
            d.shuffleCards();
        }
    }
}


程序代码:
package lab02;
/** Objects of this class represents cards in
 *    a deck (of cards).
 *    A card is immutable, i.e. once created its
 *    rank or suit cannot be changed.
 */

public class Card
{
    
    /** rank: 1 = Ace, 2 = 2, ...
     *  suit: 1 = spades, 2 = hearts, ...
     */                        
    public Card(int rank, int suit)
    {
        this.rank = rank;
        this.suit = suit;
    }
    
    public int getRank()
    {
        return rank;
    }
    
    public int getSuit()
    {
        return suit;
    }
    
    public String toString()
    {
        String info = rankTab[rank-1] + " of " + suitTab[suit-1];
        return info;
    }
    
    private int rank, suit;

    // Tables for converting rank & suit to text (why static?)
    private static final String[] rankTab = {
        "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
        "Jack", "Queen", "King"    
    };
    
    private static final String[] suitTab = {
        "clubs", "diamonds", "spades", "hearts"
    };
}



程序代码:
package lab02;

import java.util.*;

public class Deck
{
    private Card[] theCards;
    private int noOfCards;
    
    public Deck()
    {
        theCards = new Card[52];
        noOfCards = 52;
        this.fill();
    }
    

    public int getNoOfCards()
    {
        return noOfCards;
    }
    
        
    public Card getCard()
    {
        Card a = null;
     
        a = theCards[noOfCards-1];
        noOfCards--;
        return a;
    }
    

    public void shuffleCards()
    {
        Random random = new Random(); // B&ouml;r deklareras static i Deck-klassen
        Card temp;
        int pos1, pos2;                  // Two random positions
        for(int i = 0; i < 30; i++) 
        {
            pos1 = random.nextInt(noOfCards);
            pos2 = random.nextInt(noOfCards);
            // Swap
            temp = theCards[pos1];
            theCards[pos1] = theCards[pos2];
            theCards[pos2] = temp;
        }
    }
    
    private void fill()
    {

    }
}
搜索更多相关主题的帖子: 发牌 扑克牌 
2009-09-20 18:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-286293-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.234630 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved