生成构造函数

Generating a constructor

我的作业要求创建一个不接受任何输入的构造函数。构造函数初始化两个非静态属性,以便它们表示标准的一副牌或几张牌。请注意,这意味着 Cards 数组应使用 52 个元素的数组进行初始化,其中包含属于标准牌组的所有 52 张可能的卡片。您必须至少使用 1 个循环来完成此操作(也就是说,您不能编写 52 条语句来分配所有可能的值)。

提示:如果创建一个大小为 4 的字符串数组,其中包含所有可能的花色值,则可以轻松地使用两个嵌套循环来初始化纸牌数组。

我开始编写代码,但我无法理解提示的含义。我知道我必须创建一个多维数组并遍历元素,但不知道如何创建该多维数组。

这是我的代码:

public class Deck {

  // Declare the private attributes

  private Card[] cards;
  private int numberOfCardsLeft;

  // Access the private fields via public methods

  // Generate a constructor

  public Deck() {

    this.cards = new Card[][];

    // Iterate through all the elements of the array

    for (int i = 0; i < 4; i++) {

      // Iterate through all the elements of the subarrays

      for (int j = 0; j < 13; j++) {

        // code missing
      }
    }
  }
}

这是卡片 class:

public class Card {

  // Declare the private attributes

  private int cardValue;
  private String cardSuit;


  // Access the private fields via public methods

  // Generate a constuctor

  public Card(int value, String suit) {

    this.cardValue = value;
    this.cardSuit = suit.toLowerCase();

    // Check if the input is a valid playing card

    if (!(this.cardValue >= 1 || this.cardValue <= 13) && (this.cardSuit.equals("spades") || this.cardSuit.equals("hearts") || this.cardSuit.equals("diamonds") || this.cardSuit.equals("clubs"))) {

      // Throw an IllegalArgumentException

      throw new IllegalArgumentException("This is not a valid playing card!");
    }
  }

  public int getValue() {

    return cardValue;
  }

  public String getSuit() {

    return cardSuit;
  }
}

这是我的 getCards() 方法:

// A get() method that returns an array of Cards containing all the cards that are left in the deck

public Card[] getCards() {

  // Create a copy of the original array

  Card[] cardsLeft = new Card[cards.length];

  // Iterate through all the elements of the array

  for (int i = 0; i < cardsLeft.length; i++) {

    cardsLeft[i] = cards[i];
  }

  return cardsLeft;
}

首先你应该有一系列花色:

String[] suits = new String[]{"clubs", "hearts", "spades", "diamonds"};

要初始化一维数组,您可以使用以下代码:

Card[] cards = new Card[52];
for (int i = 0; i < suits.length; i++) {
    for (int j = 0; j < 13; j++) {
        cards[i * 13 + j] = new Card(j + 1, suits[i]);
    }
}

如果您需要二维数组,请使用:

Card[][] cards = new Card[4][13];
for (int i = 0; i < suits.length; i++) {
    for (int j = 0; j < 13; j++) {
        cards[i][j] = new Card(j + 1, suits[i]);
    }
}

最后 Card class 中的条件总是 false,因为 !(this.cardValue >= 1 || this.cardValue <= 13) 总是假的。我假设您正在寻找这样的东西:

if (this.cardValue < 1 || this.cardValue > 13 ||
        !(this.cardSuit.equals("spades") || this.cardSuit.equals("hearts") ||
                this.cardSuit.equals("diamonds") || this.cardSuit.equals("clubs"))) {
    throw new IllegalArgumentException("This is not a valid playing card!");
}

您的 getCards() 方法看起来不错并且按预期工作。这里还有一些复制数组的选项:

  1. Arrays.copyOf():
Card[] cardsCopy = Arrays.copyOf(cards, cards.length);
  1. System.arraycopy():
Card[] cardsCopy = new Card[cards.length];
System.arraycopy(cards, 0, copy3, 0, cards.length);
  1. Array.clone():
Card[] cardsCopy = cards.clone();