简体   繁体   English

BlackJack Java字符串[]不起作用

[英]BlackJack Java String[] not working

SO I am having a bit of trouble here in a java game of BlackJack. 所以我在BlackJack的Java游戏中遇到了一些麻烦。 I have switched from using hashmaps for the card names and values and converted it over to a String[] just because it is so much easier. 我已经不再使用哈希图来显示卡的名称和值,而是将其转换为String []只是因为它非常容易。 However, I appeared to have broken some code in it: 但是,我似乎已经破坏了其中的一些代码:

private void createDeck(int numCards, int numSuits) {

    deck = new ArrayList<Card>();
    cardUsed = new ArrayList<Card>();
    if ((numCards % numSuits) > 0) return;

    for (int i=0; i < numSuits; i++) {
        for(int j=1; j <= (numCards / numSuits); j++) {
            deck.add(new Card(new Suit(i), j + "", j));
        }
    }
} 

This won't run and gives me an unresolved compilation problem: The constructor Card(Suit, String, int) is undefined. 这将无法运行,并且给了我一个未解决的编译问题:构造函数Card(Suit,String,int)是未定义的。 However I have Card's parameters as Card(Suit, String[], int). 但是我有Card的参数作为Card(Suit,String [],int)。 Is there a way to go around this by still using a String array? 有没有办法通过仍然使用String数组来解决这个问题? It underlines the deck.add(new Card(new Suit(i), j + "", j)); 它强调了deck.add(new Card(new Suit(i), j + "", j)); in the program. 在程序中。

Here is the source for Card.java 这是Card.java的源代码

public class Card  {

private String[] cardRank = new String[] {"Ace", "Two", "Three", "Four", 
        "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"} ;

private Suit suit = null;

private int cardValue = 0;

public Card(Suit suit, String[] cardRank, int cardValue) {
    this.cardRank = cardRank;
    this.suit = suit;
    this.cardValue = cardValue;
}

public String toString() {
    return cardRank + " of " + suit.getSuitName();
}

public String[] getCardRank() {
    return cardRank;
}

public void setCardRank(String[] cardRank) {
    this.cardRank = cardRank;
}

public Suit getSuit() {
    return suit;
}

public void setSuit(Suit suit) {
    this.suit = suit;
}

public int getCardValue() {
    return cardValue;
}

public void setCardValue(int cardValue) {
    this.cardValue = cardValue;
}

} }

Well I think this is bad design generally. 好吧,我认为这通常是不好的设计。 But for this specific Question the answer would likely be to replace j + "" with new String[] {j + ""} 但是对于这个特定的问题,答案可能是用new String[] {j + ""}替换j + ""

Try this: 尝试这个:

new Card(new Suit(i), new String[]{j + ""}, j)

You were passing as second parameter j + "" , which is a String . 您将第二个参数j + ""传递给String To pass a String[] , which is the expected type, just pack that string into a single-element array: new String[]{j + ""} . 要传递String[] (这是期望的类型),只需将该字符串打包到一个单元素数组中: new String[]{j + ""}

On a more fundamental level: why is the card rank defined as a String[] in the first place? 从更基本的角度来看:为什么卡片排名首先定义为String[] it should be a String , a single card only has one rank! 它应该是String ,一张卡只有一个等级! better fix the Card class and change it to have a private String cardRank as attribute: 更好地修复Card类,并将其更改为具有private String cardRank作为属性:

public class Card  {

    private Suit suit = null;
    private String cardRank;
    private int cardValue = 0;

    public Card(Suit suit, String cardRank, int cardValue) {
        this.cardRank = cardRank;
        this.suit = suit;
        this.cardValue = cardValue;
    }

}

Now the original initialization code will work as intended: 现在,原始的初始化代码将按预期工作:

new Card(new Suit(i), j + "", j)

You are passing in a String , not a String[] . 您传递的是String而不是String[] One is just a single object, while the other is an array of objects. 一个只是一个对象,而另一个是一组对象。

j + "" results in one String , not a set of String s. j + ""一个String ,而不是一组String

You can solve this by placing the String into an array of String s before passing it into your constructor. 您可以通过将解决这个String变成数组String传递到您的构造函数之前秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM