gpt4 book ai didi

java - 尝试打印桥牌游戏时出现空错误

转载 作者:行者123 更新时间:2023-12-01 18:15:20 25 4
gpt4 key购买 nike

当我运行 main 时,我在这一行收到 java null 错误

int rank = cards[i].getRank();

int points = cards[i].getPoints();

我只是想获取手牌数组中卡片的 countHighCardPoints() 点值,并将这些点添加到总和中。如果我没有在 char suit = cards[i].getSuit(); 上也得到空值,我还将假设我的 countDistributionPoints() 会起作用。

因此我的Hand类(class)需要帮助。

public class Hand
{

//Holds an array of card objects
private Card [] cards = new Card [13];

/**
* Constructor takes array of Cards and assigns that parameter to
* the instance variable
*/
public Hand(Card [] cards)
{
Card [] hand = cards;
}

/**
* Looks through each card in hand array and adds its points
* if the card has any to a sum highPoints
*/
public int countHighCardPoints()
{
int highPoints = 0;

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

int rank = cards[i].getRank();
int points = cards[i].getPoints();

highPoints += points;

}

return highPoints;
}


public int countDistributionPoints()
{
int countPoints = 0;

for (int i = 0; i < cards.length; i++) {
//char suit = cards[i].getSuit();

if (cards[i].getSuit() >= 3)
countPoints = 0;
else if (cards[i].getSuit() == 2)
countPoints++;
else if (cards[i].getSuit() == 1)
countPoints += 2;
else if (cards[i].getSuit() == 0)
countPoints += 3;
}

return countPoints;
}

甲板等级供引用

public class Deck
{
//Holds an array of card objects
private Card [] cards = new Card [52];

//Holds number of cards remaining in deck
private int count;


/**
* Creates a Card [] arrayOfCards which is 13 cards for each player
* and will determine number of cards that was dealt with count.
*/
public Card [] dealThirteenCards()
{
Card [] arrayOfCards = new Card [13];

for (int i = 0; i <= 12 && count < 52; i++) {
arrayOfCards[i] = cards[i];
count++;
}

return arrayOfCards;
}

最佳答案

仍在研究您的代码,但目前我已经在这里确定了一些内容:

1.在你的 Hand 的构造中,你永远不会将传入的值分配给它的实例变量卡,这就是为什么每个 Hand 下的牌组将是一个空数组的原因。您应该将其更改为:

public Hand(Card [] cards) {
this.cards = cards;
}

2.Deck下的dealThirteenCards行为异常。

for (int i = 0; i <= 13 && count < 52; i++) {
arrayOfCards[i] = cards[i];
count++;
}

初始化 Deck 后,计数将为 -1。上面的代码将为 4 个玩家执行 4 次,每次为每个玩家分配 14 张牌。 如果正确结束,计数将为 -1 + 4*14 = 55,大于 52。因此,您的 for 循环将比预期更早终止。这表明你的游戏中的第四位玩家不会获得我认为的最后三张牌。

希望这有帮助。

关于java - 尝试打印桥牌游戏时出现空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30010945/

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