不确定如何修复此错误,我评论了代码中的错误,对java来说非常新。我正在尝试将我的卡片发送到我的构造函数中并创建一个卡座阵列。
public abstract class CardGame {
protected String[] deck = new String[52];
private String[] Suit = {"Spade","Dimond","Heart","Club"};
private String[] Card = {"2","3","4","5","6","7","8","9","10","jack","queen","king","ace"};
public CardGame(){
int c = 0;
for (int x=0; x < Suit.length; ++x){
for (int i = 0; i < Card.length; ++i){
deck[c] = new Card(Suit[x], Card[i]); // heres the error
c++;
}
}
}
}
public class Card {
String suit;
String name;
public Card(String s,String n){
suit = s;
name = n;
}
public String getsuit(){
return suit;
}
public String getname(){
return name;
}
}
答案 0 :(得分:4)
问题在于:
protected String[] deck = new String[52];
如果您打算将卡片插入该套牌,那么您可能希望拥有一个类型为Card的阵列,那么您有一个代表您的卡片类型为String的数组。
让你的问题行开始工作:
deck[c] = new Card(Suit[x], Card[i]);
你需要像这样声明牌组:
protected Card[] deck = new Card[52];