简体   繁体   English

二十一点游戏 - Java 的难度 - 超类

[英]Blackjack game - Difficulty with Java - superclasses

I'm creating a Blackjack program and I'm stuck, I would appreciate any help available.我正在创建一个二十一点程序,但我被困住了,我将不胜感激任何可用的帮助。 I have a Player class and a Card class.我有一个播放器 class 和一张卡片 class。

The below function (getHandValue) is meant to calculate the total value of the player's hand.下面的 function (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;
}

This method, printHand, is meant to print the contents of the player's hand and the value of the player's hand using getHandValue.此方法 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 + ")");
}

I keep getting the error that "Cannot invoke getHandValue() on the array type Card[]" in the printHand method.我在 printHand 方法中不断收到“无法在数组类型 Card[] 上调用 getHandValue()”的错误。

I'm not sure what I'm doing wrong, I'm sure its because of the way I structured these classes but I'm not sure where to even start我不确定我做错了什么,我确定这是因为我构建这些课程的方式,但我什至不知道从哪里开始

Thank you for your time感谢您的时间

You are using an Array, and you need to expect its working as an array, you need to move your hand.getHandValue();您正在使用一个数组,并且您需要期望它作为一个数组工作,您需要移动您的hand.getHandValue(); inside the loop and let the value stay outside.在循环内部,让值留在外面。

Like this:像这样:

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 + ")");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM