gpt4 book ai didi

java - 我在 Blackjack Java 中测试 Player 类有什么问题

转载 作者:行者123 更新时间:2023-12-02 00:25:06 24 4
gpt4 key购买 nike

我正在尝试正确测试我的玩家类,我几乎已经完成了,但是我的 p1.setPlayerHand 方法遇到了问题。这是我用于玩家类的以下代码:

玩家类别:

 package model;

public class Player
{
private String PlayerName;
private Hand PlayerHand;
private boolean Dealer;

public Player(String name)
{
PlayerName = name;
PlayerHand = new Hand();
Dealer = false;
}

public void setName (String name)
{
this.PlayerName = name;
}

public String getName()
{
return PlayerName;
}

public void setDealer (Boolean dealer)
{
this.Dealer = dealer;
}

public boolean getDealer()
{
return Dealer;
}

public void setPlayerHand (Hand hand)
{
this.PlayerHand = hand;
}

public void getHand()
{
PlayerHand.displayCardsinHand();
}

public static void main (String [] args)
{
Player p1 = new Player("player1");
Hand h = new Hand();
//System.out.println(p1);
p1.setName("BARRY");

System.out.println(p1.getName());
p1.setDealer(false);
System.out.println(p1.getDealer());

//this is the error that is preventing my program to run
p1.setPlayerHand(h.addCard(new Card(Suit.CLUBS, CardRank.ACE)));
p1.getHand();
}
}

我收到的以下错误(在测试 Player 类之后)是这样的:

线程“main”java.lang.Error中出现异常: Unresolved 编译问题:Player类型中的方法setPlayerHand(Hand)不适用于参数(void)在 model.Player.main(Player.java:57)

这是下面的手牌类(链接到玩家类):

手牌类别:

package model;

import java.util.Vector;
import java.util.Random;

public class Hand

{
private Vector<Card> hand;

public Hand()
{
hand = new Vector<Card>();
}

public void addCard(Card c)
{

hand.add(c);
}

public void displayCardsinHand()
{
for (int card = 0; card < hand.size(); card++)
{
System.out.println(hand.elementAt(card));
}
}

public int getCardsinHand()
{
return hand.size();
}

public Card getCard(int position)
{
if(position >= 0 && position < hand.size())
return (Card)hand.elementAt(position);
else
return null;
}

public int getScore()
{
int value = 0;
boolean ace = false;

for (int i = 0; i < hand.size(); i++)
{
Card c;
c = getCard(i);
value = value + c.getRankValue();

if(c.getRankValue() == 1)
{
ace = true;
}
}

if(ace == true && value + 10 <= 21)
{
value = value + 10;
}

return value;
}
public static void main (String [] args)
{
Hand h = new Hand();
System.out.println(h);

h.displayCardsinHand();

System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.HEARTS, CardRank.ACE));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.SPADES, CardRank.JACK));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.DIAMONDS, CardRank.QUEEN));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.CLUBS, CardRank.KING));
System.out.println(h.getCardsinHand());
System.out.println(h.getCardsinHand());
h.displayCardsinHand();
h.getCard(1);

System.out.println(h.getScore());
}

}

我曾多次尝试修改p1.setPlayerHand测试。我很感谢有关如何解决此问题的任何建议和提示,谢谢。如果我的代码对于这篇文章来说太长,那么我很乐意接受任何关于我应该做什么来缩短它的建议(以供将来引用)。如果这里有人需要查看我编写的任何其他类(这可能有助于他们帮助我解决此错误),请在此通知我,谢谢。

最佳答案

方法addCard 不会返回任何内容(void)。因此您无法将此方法的结果传递给setPlayerHand(Hand)。这就是你正在做的事情。

关于java - 我在 Blackjack Java 中测试 Player 类有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10203111/

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