Mon problème est mon professeur nous a donné des instructions très détaillées sur la façon d'écrire cette méthode:Faire face une main de cartes - erreur types incompatibles
« La méthode dealAHand a une boucle qui traite une carte (en utilisant la méthode précédente - dealACard) à la fois jusqu'à ce qu'il ait traité des HandSize Cards La chaîne renvoyée est une liste des objets Card dans la main - similaire au résultat de toString sauf que la String retournée ici contient uniquement des objets handSize Card. J'essaie de faire ce qu'il a dit, mais je continue à obtenir des erreurs de types incompatibles: dans cette incarnation, il est dit que crd ne peut pas être converti en Card []. De plus, c'est la seule fois que la variable handSize est mentionnée.
Je peux également poster mon pilote si nécessaire. Des idées? S'il vous plaît gardez-les intro-niveau.
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;
}
}
Où exactement obtenez-vous cette erreur? – Mureinik
crd est null dans dealAHand. –
L'erreur est sur retour crd; – Tonantzin