我正在尝试正确测试我的播放器类,我几乎已经完成了但是我的 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();
}
}
我收到的以下错误(在测试播放器类之后)是:
线程“main”中的异常java.lang.Error:未解决的编译问题:Player类型中的方法setPlayerHand(Hand)不适用于参数(void) 在model.Player.main(Player.java:57)
这是下面的Hand Class(链接到Player Class):
手类:
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 测试。我很感激有关如何解决这个问题的任何建议和提示,谢谢。 如果我的代码对于这篇文章来说太长了,那么我很乐意接受任何关于我应该做些什么的建议来缩短它(以备将来参考)。 如果有人要求我看到我写的任何其他课程(这可能有助于他们帮助我解决这个错误),那么请在这里通知我,谢谢。
答案 0 :(得分:2)
方法addCard
不返回任何内容(void)
。因此,您无法将此方法的结果传递给setPlayerHand(Hand)
。这就是你正在做的事情。
答案 1 :(得分:1)
如果更改
,代码应该编译并运行p1.setPlayerHand(h.addCard(new Card(Suit.CLUBS, CardRank.ACE)));
到
h.addCard(new Card(Suit.CLUBS, CardRank.ACE));
p1.setPlayerHand(h);
这是因为setPlayerHand
方法需要传递Hand
类型的对象,但addCard
方法不返回任何内容(它被声明为void
)