我正在研究Java项目-扑克牌。

而且,在这个Card.java类中,我已经声明并初始化了变量和数组列表。我遇到了这个错误。这些错误在每种getter和setter方法下都有。

error: incompatible types: String cannot be converted to String[]
error: incompatible types: int[] cannot be converted to int

public class Card {

   // private String suit;
    //private String name;
    //private int value;

private String[] suit = {"spades","hearts","clubs","diamonds"};
private String[] name = {"Ace","Jack","Queen","King"};
private int[] value = {1,2,3,4,5,6,7,8,9,10,11,12,13};

public Card(String s, String n, int v)
{
    suit = s;
    name = n;
    value = v;
}

public String getSuit()
{
    return suit;
}

public String getName()
{
    return name;
}

public int getValue()
{
    return value;
}

public String toString()
{
    return "<"+suit+" "+name+">";
}


这是全班。

希望任何人都知道并且可以帮助我,谢谢! (:

如果您不了解我要达到的目标,请告诉我,尽我所能解释

最佳答案

我敢打赌,您为这些初始字段集生成了getter和setter以及构造函数。

// private String suit;
// private String name;
// private int value;


但是将它们更改为

private String[] suit = { "spades", "hearts", "clubs", "diamonds" };
private String[] name = { "Ace", "Jack", "Queen", "King" };
private int[] value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };


您忘记了对其进行相应的修改。您需要将getter,setter和构造函数更改为类似的内容。 toString()方法也是如此。

public class Card {

    private String[] suit = { "spades", "hearts", "clubs", "diamonds" };
    private String[] name = { "Ace", "Jack", "Queen", "King" };
    private int[] value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

    public Card(String[] suit, String[] name, int[] value) {
        super();
        this.suit = suit;
        this.name = name;
        this.value = value;
    }

    public String[] getSuit() {
        return suit;
    }

    public void setSuit(String[] suit) {
        this.suit = suit;
    }

    public String[] getName() {
        return name;
    }

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

    public int[] getValue() {
        return value;
    }

    public void setValue(int[] value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Card [suit=" + Arrays.toString(suit) + ", name="
            + Arrays.toString(name) + ", value=" + Arrays.toString(value)
            + "]";
    }
}


如果您碰巧更改了类中的字段,请务必记住要生成新的getter,setter,构造函数和toString()方法。

10-08 02:34