カードの手札を手に入れようとしていますが、手札はシャッフルしています。私の問題は、initialDeal()メソッド(カードの手を描く必要があるがシャッフルした状態)でshuffleDeck()を実行すると、ArrayIndexOutOfBounds
の例外が出ます。カードの手札を描く手助けが必要です
そして、私はクラブの2つのナルフを描いています...ノートは基本的に0であり、ちょうどプレースホルダーです。それは使用されません。
class Card {
int suit, rank;
public Card() {
this.suit = 0; this.rank = 0;
}
public Card (int suit, int rank) {
this.suit = suit; this.rank = rank;
}
public int getSuit() {
return suit;
}
public int getRank() {
return rank;
}
public void printCard() {
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };
System.out.println (ranks[rank] + " of " + suits[suit]);
} //end printCard
} //end class
カードのクラス(これは基本的には基本的なものをやっている)^
import java.util.Random;
class Hand {
static Card[] deck = new Card[52];
Card[] hand = new Card[10];
static int index;
public static void createDeck() {
int m = 0;
for (int j = 0; j < 4; j++) {
for (int k = 1; k < 14; k++) {
deck[m] = new Card(j,k);
m++;
}
}
index = 0;
} // end createDeck
public static Card deal() {
Hand.index++;
return deck[index-1];
} // end deal
public static int compareCard (Card c1, Card c2) {
if (c1.getSuit() > c2.getSuit()) return 1;
if (c1.getSuit() < c2.getSuit()) return -1;
if (c1.getRank() > c2.getRank()) return 1;
if (c1.getRank() < c2.getRank()) return -1;
return 0;
} // end compareCard
public void printDeck (int size) {
int j = 0;
while (j < size) {
deck[j].printCard();
j++;
}
}// end printDeck
public static void shuffleDeck() {
Card tempCard;
Random rd = new Random();
for (index = 0; index < deck.length; index++) {
int r = rd.nextInt(deck.length);
tempCard = deck[index];
deck[index] = deck[r];
deck[r] = tempCard;
}
} // end shuffleDeck
public void initialDeal() {
hand[0] = null;
hand[1] = null;
hand[0] = deal();
hand[1] = deal();
}
public void printHand() {
initialDeal();
index = 0;
for(Card outputCard = new Card(); hand[index] != null; index++) {
outputCard.printCard();
}
}
}
ハンドクラス^(私はシャッフルデッキからカードを2枚引く必要がある場所です)
class Dealer {
Hand dealer = new Hand();
public Dealer() {
dealer.createDeck();
dealer.shuffleDeck();
System.out.println("Dealer's Hand");
System.out.println("");
initDeal();
}
public void initDeal() {
dealer.initialDeal();
dealer.printHand();
}
} //end class
ディーラークラス。^手の方法を呼び出す
class Driver {
public static void main (String[] args) {
Dealer dealer = new Dealer();
} //end main method
} //end class
は^あなたはshuffleDeck
でindex
を修正しているので、shuffleDeck
終了後、index
がshuffleDeck.length
である基本的に
これを絞り込んでください。例外は、問題の発生場所を正確に示しています。 – Mat
私はもう例外はありません。 – user1283885
デバッガの使い方を学ぶ時間。それは投資の価値があるので、重要なスキルです。 http://www.eclipse.org/resources/resource.php?id=405 – Gray