我的作业要求创建一个无需输入的构造函数。构造函数初始化两个非静态属性,以使其代表标准的牌组或卡牌。请注意,这意味着应使用52个元素的数组初始化纸牌数组,其中包含52个属于标准牌组的所有可能的纸牌。您必须至少使用1个循环来完成此操作(也就是说,您不能编写52条语句来分配所有可能的值)。
提示:如果创建一个大小为4的String数组,并在其中包含所有可能的suits值,则可以轻松地使用两个嵌套循环来初始化纸牌数组。
我开始编写代码,但是我很难理解提示的含义。我知道我必须创建一个多维数组并遍历所有元素,但是无法弄清楚如何创建该多维数组。
这是我的代码:
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
}
}
}
}
这是Card类:
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;
}
答案 0 :(得分:1)
首先,您应该准备一套西服:
label_W = label.get_width() * new_size[0] // original_size[0]
label_H = label.get_height() * new_size[1] // original_size[1]
label_X = pos_W * new_size[0] // original_size[0]
label_Y = pos_H * new_size[1] // original_size[1]
screen.blit(pygame.transform.scale(label, (label_W, label_H)), (label_X, label_Y))
要初始化一维数组,可以使用以下代码:
scale_x, scale_y = (new_size[0] / original_size[0], new_size[1] / original_size[1])
label_size = (int(label.get_width() * scale_x), int(label.get_height() * scale_y))
label_pos = (int(pos_W * scale_x), int(pos_H * scale_y))
screen.blit(pygame.transform.scale(label, label_size), label_pos)
如果需要二维数组,请使用:
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
始终为false。我认为您正在寻找这样的东西:
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()
Card[] cardsCopy = Arrays.copyOf(cards, cards.length);
:Card[] cardsCopy = new Card[cards.length];
System.arraycopy(cards, 0, copy3, 0, cards.length);