【问题标题】:Blackjack game - Difficulty with Java - superclasses二十一点游戏 - Java 难度 - 超类
【发布时间】:2021-05-29 08:16:30
【问题描述】:

我正在创建一个二十一点计划,但我遇到了困难,如果能提供任何帮助,我将不胜感激。 我有一个 Player 类和一个 Card 类。

下面的函数(getHandValue)用于计算玩家手牌的总价值。

/*getHandValue - accessor that computes the total value of the 
player's current hand
*/
public int getHandValue(Card[] hand){
    int total = 0;

    for (Card card : hand){
        String rank = card.getRankName();
        total += card.getValue();

        if (rank.equals("Ace") && total < 11){
            total += 10;
        }
    }
    return total;
}

此方法 printHand 用于打印玩家手牌的内容以及使用 getHandValue 的玩家手牌的价值。

/* ISSUES
* printHand - accessor that prints the current contents 
of player's hand followed by value of player's hand
*/
public static void printHand(Card hand[]){
    int value = hand.getHandValue();

    for(int i = 0; i<hand.length; i++){
        System.out.println(hand[i] + "  ");
    }

    System.out.print("(value = " + value + ")");
}

我在 printHand 方法中不断收到“无法在数组类型 Card[] 上调用 getHandValue()”的错误。

我不确定我做错了什么,我确定是因为我构建这些课程的方式,但我什至不知道从哪里开始

感谢您的宝贵时间

【问题讨论】:

  • 为什么你认为 Card 数组有一个.getHandValue() 方法?请提供一个证明错误的minimal reproducible example,包括Card 类的定义。

标签: java arrays blackjack


【解决方案1】:

您正在使用一个数组,并且您需要期望它作为一个数组工作,您需要将您的 hand.getHandValue(); 移动到循环内并让值留在外面。

像这样:

public static void printHand(Card hand[]){
    int value = 0;
    for(int i = 0; i<hand.length; i++){
        value += hand[i].getHandValue();
        System.out.println(hand[i] + "  ");
    }
    System.out.print("(value = " + value + ")");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-08
    • 2023-03-21
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2022-11-12
    相关资源
    最近更新 更多