我的问题是我的教授给我们如何写这个方法非常详细的说明:交易一手牌 - 不兼容的类型错误
“的dealAHand方法有一个循环,涉及一个卡(使用以前的方法 - dealACard),直到它已经处理了handSize Cards。返回的String是手中的Card对象列表 - 类似于toString的结果,除了此处返回的String仅包含handSize Card对象。“
I试图做他所说的话,但我不断遇到不兼容的类型错误:在这个化身中,它说crd不能转换为Card []。此外,这是唯一提到变量handSize的时候。
如果需要,我也可以发布我的驱动程序。有任何想法吗?请保持他们的介绍水平。
public class Card {
private Face face;
private Suit suit;
public Card() {//default
face = Face.ACE;
suit = Suit.HEARTS;
}
public Card(Card existingCard) {//copy
//Card card = new Card(existingCard);
face = existingCard.face;
suit = existingCard.suit;
}
public Card(int n) {//professor written paramaterized
face = Face.values()[n % 13];
suit = Suit.values()[n % 4];
}
public String toString() {
String str = "";
str = "the " + face + " of " + suit;
return str;
}
}
import java.util.Random;
public class Deck {
Card[] deck;
int nextCard;
public Deck() {//default, filling the deck
nextCard = 0;
deck = new Card[52];
for (int i = 0; i < 52; i++) {
deck[i] = new Card(i);
}
}
public Deck(Card[] existingCard) {//copy
for (int i = 0; i < 52; i++) {
deck[i] = existingCard[i];
}
}
public void shuffle() {
Random rn = new Random();
Card temp;
int num;
for (int i = 0; i < 52; i++) {
num = rn.nextInt(52);
temp = deck[i];
deck[i] = deck[num];
deck[num] = temp;
}
nextCard = 0;
}
public Card dealACard() {
nextCard++;
return deck[nextCard - 1];
}
public String dealAHand() {//CHANGED
int handSize = 5;
Card[] crd = new Card[handSize];
String hand = "";
for (int i = 0; i < handSize; i++) {
hand += crd[i].dealACard() + "\n";
}
return hand;
}
public String toString() {
String str = " ";
for (int i = 0; i < 52; i++) {
str += deck[i].toString() + "\n";
}
return str;
}
}
你究竟在哪里得到这个错误? – Mureinik
crd在dealAHand中为null。 –
错误返回crd; – Tonantzin