0

我正在尝试正确测试我的播放器类,我几乎已经完成了,但是我的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 中的异常:未解决的编译问题:Player 类型中的方法 setPlayerHand(Hand) 不适用于 model.Player.main(Player.java:57) 处的参数 (void)

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

手类:

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测试。感谢您提供有关如何解决此问题的任何建议和提示,谢谢。如果我的代码对于这篇文章来说太长了,那么我很乐意接受任何关于我应该做些什么来缩短它的建议(以供将来参考)。如果这里有人需要查看我编写的任何其他类(这可能有助于他们帮助我解决此错误),请在此处通知我,谢谢。

4

2 回答 2

2

该方法addCard不返回任何东西(void)。所以你不能将此方法的结果传递给setPlayerHand(Hand). 这就是你正在做的事情。

于 2012-04-18T05:16:29.137 回答
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)。

于 2012-05-01T13:17:41.747 回答