我试图创建一个纸牌游戏高尔夫来娱乐并练习我的 Java 技能。我正在尝试对我的卡值和套件使用枚举。这些值保存在 Card 类中名为 Card 的构造函数中。
我遇到的问题是打印包含我所有个人卡片的 arraylist Deck。该方法可以在 DeckOfCards 类中找到。我想看看我的程序是否正在创建一副完整的牌。预先感谢您的帮助!
DeckOfCards 类
package finalProjectGolf;
// Deck class represent a deck of playing cards.
import java.util.ArrayList;
import java.util.Collections;
public class DeckOfCards {
ArrayList<Card> deck = new ArrayList<Card>(); //array of Card objects
private int currentCard; // index of next Card to be dealt (0-51)
private static int numDecks = 1;
public int getCurrentCard() {
return currentCard;
}
public void setCurrentCard(int currentCard) {
this.currentCard = currentCard;
}
private static final int NUMBER_OF_CARDS = 52 * numDecks; //constant # of Cards
//constructor fill deck of Cards
public DeckOfCards(){
currentCard = 0; //set currentCard so first Card dealt is deck[0]
//Card Index
int c = 0;
//for each deck
for (int d = 0; d < numDecks; d++){
//for each suit
for (int s = 0; s < 4; s++){
// for each number
for (int n = 1; n <= 13; n++){
//add a new card to the deck
deck.add(new Card(CardValue.values()[n],Suit.values()[s])); //when using Enums java makes arrays automatically and you can use them by .values()
c++;
}}}//end for loop
}//end DeckOfCards constructor
//shuffle deck of Cards with one-pass algorithm
public void shuffle() {
Collections.shuffle(deck);
}
public int points(){
int value = deck.get(currentCard).getCardValue().getCardValue();
return value;
}
//deal one Card
public Card dealCard(int currentCard) {
//determine whether Cards remain to be dealt
if( currentCard < deck.size() ){
return deck.get(currentCard); //return current Card in array
}
else
return null; // return null to indicate that all Cards were dealt
}//end method dealCard
public void printDeck(){
{
currentCard = 0; //set currentCard so first Card dealt is deck[0]
//Card Index
int c = 0;
//for each deck
for (int d = 0; d < numDecks; d++){
//for each suit
for (int s = 0; s < 4; s++){
// for each number
for (int n = 1; n <= 13; n++){
//add a new card to the deck
System.out.printf(""); //when using Enums java makes arrays automatically and you can use them by .values()
c++;
}}}//end for loop
}//end DeckOfCards constructor
}
}// end class DeckOfCards
卡片类别
package finalProjectGolf;
public class Card
{
private Suit suit;
private CardValue cardValue;
public Card (CardValue cardValue, Suit suit) //constructor of Card, holds Card value and it's suit
{
this.cardValue = cardValue;
this.suit = suit;
}
public Suit getSuit()
{
return suit;
}
public void setSuit(Suit suit)
{
this.suit = suit;
}
public CardValue getCardValue()
{
return cardValue;
}
public void setCardValue(CardValue cardValue)
{
this.cardValue = cardValue;
}
public String toString(){
return cardValue + " of " + suit;
}// end method toString
}
CardValue 类
package finalProjectGolf;
public enum CardValue
{
ACE(1),
TWO(2),
THREE(3),
FOUR(4),
FIVE(5),
SIX(6),
SEVEN(7),
EIGHT(8),
NINE(9),
TEN(10),
JACK(11),
QUEEN(12),
KING(13);
private int cardValue;
private CardValue (int value)
{
this.cardValue = value;
}
public int getCardValue() {
return cardValue;
}
}
西装类别
package finalProjectGolf;
public enum Suit
{
HEARTS(1),
SPADES(2),
CLUBS(3),
DIAMONDS(4);
private int suit;
private Suit (int value)
{
this.suit = value;
}
public int getCardSuit() {
return suit;
}
}
最佳答案
要添加到 AaronB 的答案中,您的 printDeck 方法实际上是错误的,正如您所发布的那样。目前它打印空字符串 52 次。此外,您不需要仅仅为了打印牌组中的所有项目而执行三次 for 循环。一个非常简单的实现是将单副牌的每张牌打印在新行上:
public void printDeck() {
for(Card card : deck) {
System.out.println( card.toString() );
}
}
您还需要重写枚举值的 toString 方法,以便它们打印您想要的名称。看看https://stackoverflow.com/a/14413605/1425014了解如何做到这一点。
您的主类称为 DeckOfCards。这对我来说表明该类代表一副牌。然而,从 for (int d = 0; d < numDecks; d++)
来看和private static int numDecks = 1
,看来您打算让 DeckOfCards 代表一副或多副牌。如果您需要的不仅仅是 DeckOfCards,而不是使 DeckOfCards 类复杂化,那么简单地使用集合(例如 ArrayList)可能会更清楚。
最后,在此处发布代码之前,您应该尝试确保您的评论有意义。从 DeckOfCards 构造函数复制/粘贴后, printDeck() 函数的注释没有更改。
关于java - 似乎无法打印充满对象/枚举的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20486871/