类卡不会转换为字符串

时间:2019-05-21 17:29:30

标签: java

我正在研究基本的扑克应用程序。 Card和DeckOfCards类在大多数情况下似乎可以正常工作,但是当我尝试处理社区卡(板卡)时,出现编译器错误。我有些困惑,因为似乎在交易玩家的手时,我在上一个for循环中所做的完全相同。

card.java:

public class Card {
    private final String face;
    private final String suit;

    public Card(String cardFace, String cardSuit) {
        this.face = cardFace;
        this.suit = cardSuit;
    }

    public String toString() {
        return face + suit;
    }
}

DeckOfCards.java:


public class DeckOfCards {

    private static final SecureRandom randomNumbers = new SecureRandom();
    private static final int NUMBER_OF_CARDS = 52;

    private Card[] deck = new Card[NUMBER_OF_CARDS];
    private int currentCard = 0;

    public DeckOfCards() {
        String[] faces = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"};
        String[] suits = {"h", "c", "d", "s"};

        for(int count = 0; count < deck.length; count++) {
            deck[count] = new Card(faces[count % 13], suits[count/13]);
        }
    }

    public void shuffle() {
        currentCard = 0;

        for (int first = 0; first < deck.length; first++) {
            int second = randomNumbers.nextInt(NUMBER_OF_CARDS);

            Card temp = deck[first];
            deck[first] = deck[second];
            deck[second] = temp;
        }
    }

    public Card dealCard() {
        if (currentCard < deck.length) {
            return deck[currentCard++];
        }
        else {
            return null;
        }
    }
}

DeckOfCardsTest.java:


public class DeckOfCardsTest {
    public static void main(String[] args) {
        DeckOfCards myDeckOfCards = new DeckOfCards();
        myDeckOfCards.shuffle();

        Scanner input = new Scanner(System.in);

        System.out.printf("Enter number of players(max 10): ");
        int numPlayers = input.nextInt();

        if(numPlayers > 10)
            numPlayers = 10;

        String[] playerHand = new String[10];
        String[] boardCards = new String[5];

        for(int i = 0; i < numPlayers; i++) {
            playerHand[i] = myDeckOfCards.dealCard() + " " + myDeckOfCards.dealCard();
            System.out.printf("Player %d Hand: %s%n", (i + 1), playerHand[i]);
        }

        for(int i = 0; i < 5; i++) {
            boardCards[i] = myDeckOfCards.dealCard();
                        //^This is the line that throws the error

        }

        System.out.printf("Flop: %s %s %s%n", boardCards[0], boardCards[1], boardCards[2]);
        System.out.printf("Turn: %s%n", boardCards[3]);
        System.out.printf("River: %s%n", boardCards[4]);



    }
}

编译器错误为:

DeckOfCardsTest.java:25: error: incompatible types: Card cannot be converted to String
boardCards[i] = myDeckOfCards.dealCard();
                                      ^

4 个答案:

答案 0 :(得分:1)

在第一个for循环中,您正在执行myDeckOfCards.dealCard() + " " + myDeckOfCards.dealCard(),使用+ " "会将Card返回的dealCard类转换为String。

因此,要解决您的问题,您可以执行dealCard().toString()dealCard() + ""

答案 1 :(得分:0)

作为异常情况,在DeckOfCardsTest代码的第25行中,您将类型为Card的dealCard()方法的结果分配给类型为String的bo​​ardCards数组的元素。 Java不会将自定义类隐式转换为字符串。 您可以将boardCards的类型从String []更改为Card []。 或者,您可以这样做:

boardCards[i] = myDeckOfCards.dealCard().toSring();

答案 2 :(得分:0)

myDeckOfCards.dealCard()返回一个Card对象,而不是String,并且您不能直接将Card对象转换为String。看来您想将数组声明为Card数组,而不是String数组。

Card[] playerHand = new Card[10];
Card[] boardCards = new Card[5];

如果您确实需要String,请在toString()对象上调用Card。将对象连接到字符串时,有一个隐式toString()调用,但不是直接分配。

for(int i = 0; i < numPlayers; i++) {
                playerHand[i] = myDeckOfCards.dealCard() + " " + myDeckOfCards.dealCard();
                System.out.printf("Player %d Hand: %s%n", (i + 1), playerHand[i]);
            }

for(int i = 0; i < 5; i++) {
    boardCards[i] = myDeckOfCards.dealCard().toString();  //need to explicitly call toString() here.
    //^This is the line that throws the error

            }

出于相同原因,请参见下面的此测试代码将无法编译。第一行可以,但第二行则不能,因为这是直接分配,而不是串联。

public class Test { 
  public static void main(String[] args) throws Exception {
      String test="blah" + new Integer(7); //this is OK.  Implicit toString() call
      String test2=new Integer(7); //compile error here.
  }
}

答案 3 :(得分:0)

使用.toString()方法,您可以将其转换为字符串类型,一切都会好起来的。 在错误行上使用它。

boardCards[i] = myDeckOfCards.dealCard().toSring();