【发布时间】: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类的定义。