gpt4 book ai didi

java - 从卡牌数组中调用一张卡牌

转载 作者:行者123 更新时间:2023-12-01 10:52:17 25 4
gpt4 key购买 nike

我正在进行一项关于创建一副纸牌的 Java 实验,但在其中一个部分遇到了问题。对于这一部分,我必须有一个接受整数参数并返回 Card 对象的方法。它应该返回对牌组数组中(0 到 NUMBERS-1)中的卡片对象的引用。如果索引不在范围内,则必须返回 null。我尝试使用牌组[输入参数]来做到这一点,但意识到这不起作用,因为牌组是纸牌。我不确定该怎么做。我应该改变什么才能让它正常工作?谢谢!

public class DeckOfCards {
public static final int NUMBERS = 52;
public Card[] deck = new Card[NUMBERS];

String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};

public DeckOfCards() {
int i = 0;
for(String rank : ranks) {
for(String suit : suits) {
deck[i] = new Card(rank, suit);
i++;
}
}

}

public Random random = new Random();

public void shuffle() {
for(int i = 0; i < 500; i++) {
int firstRandomCard = random.nextInt(NUMBERS - 1);
int secondRandomCard = random.nextInt(NUMBERS - 1);

Card firstCard = deck[firstRandomCard];
Card secondCard = deck[secondRandomCard];

deck[firstRandomCard] = secondCard;
deck[secondRandomCard] = firstCard;

}
}

//this is where I am having problems.
public int getCardAt(int number) {
if (number < 0 || number > NUMBERS - 1)
return null;
else
return deck[number];
}
}

最佳答案

我认为您的 getCardAt 方法签名是错误的。

您希望它返回一个 Card 而不是 int

关于java - 从卡牌数组中调用一张卡牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33790314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com