它不断抛出这个异常,但它显示在程序的输出之间,到目前为止,它完全按照我的需要运行。可能是什么问题?
好吧,这就是它引发异常的地方: public class RunGame {
public static void main(String[] args) {
//Create players
Hand Player1 = new Hand(5);
Hand Player2 = new Hand(5);
Hand Player3 = new Hand(5);
Hand Player4 = new Hand(5);
Deck deck = new Deck();
Card print = new Card(1,'c');
deck.Shuffle();
for (int i = 0; i <= 52; i++){
print = deck.getCard(i); //**THROWS HERE**
System.out.println(print.toString());
}
然后在我的甲板课上有:
public class Deck {
private char suit;
private int value;
private Card [] deck = new Card[52];
public Deck(){
int count = 0;
for(int i = 1; i <= 4; i++) {
if (i == 1)
{
suit = 'C';
}
else if (i == 2) {
suit = 'D';
}
else if (i == 3) {
suit = 'H';
}
else {
suit = 'S';
}
for (int x = 1; x <= 13; x++){
deck[count] = new Card(x, suit);
count++;
}
}
}
和
public Card getCard(int i){
int v = deck[i].getValue(); //**AND HERE**
char s = deck[i].getSuit();
Card temp = new Card(v,s);
return temp;
}
我只是想打印甲板以确保它正确洗牌。一切都打印得很好,但它仍然表明了这一点。(我的卡片组对象是一个 52 张卡片的数组,getValue 和 getCard 方法在 Card 类中。)