Java处理5张随机卡片,显示卡片中剩余的卡片数量

时间:2014-08-03 21:55:42

标签: java class methods return-value getter-setter

我昨天发布了这项任务,你们帮助了我很多!我已经能够让我的程序工作了,我把它清理干净了(我想)。我还有最后一件事要做,我老实说不知道怎么回事。

分配是创建一个CardGame驱动程序类,然后创建一个" card"在一个卡片类,然后是一副卡片"在DeckOfCards课程中,随机播放并交易5张随机卡片,然后发出卡片并报告卡片中留下的卡片数量#34;。引号中的最后一行是我不知道该怎么做。有什么建议?

另外,我通过一些建议重命名了很多变量,并且赋值声明"确保编写适当的构造函数,getter,setter,toString和其他两个类所需的方法。"我认为他们是合适的,你们觉得怎么样?我应该将他的一些方法改为吸气者和制定者吗?

这是我的代码。我试图评论所有内容以保持对我的理解,如果它太多了就很抱歉......

import java.util.Random;

public class CardsGameTest {

//execute application.
public static void main(String[] args) {

    DeckOfCards myDeck = new DeckOfCards();
    myDeck.shuffle(); //shuffle cards.

       //Deal 5 random cards.   
       for (int i = 0; i < 5; i++) {
           myDeck.dealCard();
           System.out.println("Deal a card: " + myDeck.dealCard());  
       }//end of for loop
   }//end main method
}//end of CardsGameTest class

class Card {

private String face;
private String suit;

public Card(String cardFace, String cardSuit) {
    face = cardFace;
    suit = cardSuit;
}//end two-argument Card constructor.

//return string representation of card.
public String toString() {
    return face + " of " + suit;
  }//end toString method.
}// end class card.

class DeckOfCards {
    private Card deck []; //array of card objects
    private int currentCard;  //index of next card to be dealt.
    private final int NUMBER_OF_CARDS = 52;  //Constant # of cards.
    private Random randomNumbers;  //random # generator.

//Constructor fills deck of cards.
public DeckOfCards () {
    String faces [] = { "Ace", "Two", "Three", "Four", "Five", "Six",
            "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    String suit [] = {"Hearts", "Diamonds", "Clubs", "Spades"};

    deck = new Card[NUMBER_OF_CARDS];  //create an array of card objects.
    currentCard = 0;  //Set currentCard so first card dealt is deck[0].
    randomNumbers = new Random(); //create random number.

    //Fill deck with card objects.
    for (int count = 0; count < deck.length; count++) {
        deck[count] = new Card(faces[count % 13], suit[count / 13]);
    } //end for loop    
}//end DeckOfCards constructor.

//Shuffle deck of Cards
public void shuffle() {

    //after shuffling, dealing should start at deck[0] again.
    currentCard = 0;  //initialize currentCard again.

    //for every Card, pick another random Card and swap places "shuffling"
    for (int first = 0; first < deck.length; first++) {
        //select a random number between 0 and 51
        int second = randomNumbers.nextInt(NUMBER_OF_CARDS);

        //swap current card w/ a random card
        Card temp = deck[first];
        deck[first] = deck[second];
        deck[second] = temp;
    }//end for loop     
}//end shuffle method.

//deal one Card
public Card dealCard() {

    //see if cards remain to be dealt.
    if (currentCard < deck.length) {
        return deck[currentCard++]; //return currentCard in array.
    } else {
        return null; //if all cards are dealt.
    }//end conditional.
  }//end method dealCard.
}//end class DeckOfCards.   

说明书还说&#34;打印每张卡片,并在#34;这是指一次一张5张卡吗?那种困惑我。无论如何,我想在驱动程序类中我可以为for循环添加一个for循环,并且当它处理一张卡时它可以通过第二个for循环并打印在牌组中剩下多少张牌。我混淆了自己试图解决这个问题。无论如何,谢谢你的帮助!!

斯科特

0 个答案:

没有答案