我几乎完成了我的项目,我收到了这个错误:
“表达式的类型必须是数组类型,但它已解析为卡”
它的内容如下:deck[i] = newCard[i];
以下是代码:
public class Deck
{
//nextCard is used to keep count of which number card you are on in the deck.
public int nextCard;
private static final int DECK_SIZE = 52;
//deck is used to represent an entire deck of cards.
Card[] deck;
//hand is a pair of 7 cards; is generated by dealACard();
Card[] hand;
/**
* Default Constructor
*
* <hr>
* Date created: Feb 17, 2014
*
*
*/
public Deck ( )
{
int i;
nextCard = 0;
for(i=0;i<DECK_SIZE;i++)
{
Card newCard = new Card(i);
deck[i] = newCard[i];
}
}
我做错了什么?
答案 0 :(得分:2)
删除数组括号以使用刚刚创建的Card
实例
deck[i] = newCard;
^
答案 1 :(得分:2)
代码
deck[i] = newCard[i];
意思是:
Put the object at index i from my Array newCard into index i in my Array deck
你真正想要的是
Put this object newCard into index i in my Array deck
因为newCard
不是数组,所以它是Card
对象,它是您的数组deck
所拥有的类型。只需将您的代码更改为:
deck[i] = newCard;