2

我是一个试图学习 Java 的相当笨的人,我在完成我给自己的任务时遇到了一点麻烦。基本上我正在尝试在本页末尾进行练习。

我设法完成了三节课。卡片

public class Card {

public int nRank; // Used later
public int maxRank = 13; //The max number of Ranks
public int nSuit; // Used later
public int maxSuit = 4; // Max number of suits


//Associate both rank and suit numbers with strings

public String[] ranks = new String[maxRank - 1];
{
    ranks[0] = "two";
    ranks[1] = "three";
    ranks[2] = "four";
    ranks[3] = "five";
    ranks[4] = "six";
    ranks[5] = "seven";
    ranks[6] = "eight";
    ranks[7] = "nine";
    ranks[8] = "ten";
    ranks[9] = "Jack";
    ranks[10] = "Queen";
    ranks[11] = "King";
    ranks[12] = "Ace";

    }

public String[] suits = new String[maxSuit - 1];
{
    suits[0] = "Clubs";
    suits[1] = "Diamonds";
    suits[2] = "Spades";
    suits[3] = "Hearts";
    }

public String suit = suits[nSuit]; //The suit string of the card whose suit number is nSuit
public String rank = ranks[nRank]; //Same but with ranks

//Constructor for the Card object, with two arguments, x for rank, y for suit
public Card(int x,int y){
    this.nRank = x;
    this.nSuit = y;
}

//method to get which card it is in a string
public String whatCard(){
    return rank + " of " + suit;
}
}

甲板:

    public class Deck {

public static int nRanks = 13; //number of ranks
public static int nSuits = 4; // number of suits
public static int nCard = nRanks * nSuits; // number of cards

Card[] deck = new Card[nCard -1]; //new array called deck to store all the cards 
int h = 0; //a variable to control the place of each card in the array
//constructor for the Deck
public Deck() {

while(h < 52){ // loop until there are 52 cards

// cycles through all the possible combinations between i(ranks) and j(suits) and creates a card with each
for(int i = 1; i <= nRanks; i++){

    for(int j = 1; j <= nSuits; j++){

        deck[h] = new Card(i,j); // creation of the card
        h++; // adds 1 to to h so the program knows how many cards are there
        }
    }

}
}
//method for getting a card depending on its position in the array(x)
    public Card getCard(int x){
    return deck[x-1];   
}
}

还有我称之为Shuffle的卡片/甲板的展示器:

public class Shuffle {

public static void main(String[] args){
        Deck newDeck = new Deck(); // creates a new Deck object
        //loops through all the cards in the deck
    for(int i = 0; i < Deck.nCard; i++){
        System.out.println(newDeck.getCard(i).whatCard()); // prints each card
    }

}

}

虽然 eclipse 没有注意到代码中的任何错误,但当我尝试编译时,我看到了这个:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
    at Card.<init>(Card.java:26)
    at Deck.<init>(Deck.java:20)
    at Shuffle.main(Shuffle.java:5)

我错过了什么?

4

5 回答 5

3

在所有长度为 N 的循环中,您正试图从元素 1..N 访问。您应该从 0..(1-N) 循环。

例如,如果您有 4 套西装,则您有一个包含元素 0、1、2、3 的数组。通过说(for j=1; j<=4, j++),您正在尝试访问超出范围的元素 4。

它应该是(j=0; j<4; j++).

于 2012-06-01T23:56:17.150 回答
2

rank[] 和 suits[] 的索引都从 0 到 12。

您正在尝试从 1 访问到 13 (在您的 for 循环中)

使用此代码:

for(int i = 0; i < nRanks; i++){

    for(int j = 0; j < nSuits; j++){

        deck[h] = new Card(i,j); // creation of the card
        h++; // adds 1 to to h so the program knows how many cards are there
    }
}

编辑 :

我注意到另一个错误,即生成越界的错误:在您的 Card 类中,更改以下行:

public String[] ranks = new String[maxRank - 1];
public String[] suits = new String[maxSuit - 1];

经过

public String[] ranks = new String[maxRank];
public String[] suits = new String[maxSuit];

创建数组时,不指定最后一个索引,而是指定可用的位置。因此,如果您想输入 13 个值,请指定新的 String[13]。


编辑:完整的甲板课程:

public class Deck {

   public static int nRanks = 13;  
   public static int nSuits = 4; 
   public static int nCard = nRanks * nSuits; 

   Card[] deck = new Card[nCard]; //nCard indexes, not nCard - 1

   public Deck() {
      //remove the while, double loop useless
      for(int i = 0; i < nRanks; i++){
         for(int j = 0; j < nSuits; j++){
            deck[j * nRanks + i] = new Card(i,j);
         }
      }
   }

   public Card getCard(int x){
      return deck[x-1];   
   }
}
于 2012-06-01T23:56:34.753 回答
2

Java 数组是从零开始的......因此创建一个包含 13 - 1 == 12 个元素的新数组,但数组从零开始,这意味着访问元素“12”实际上是第 13 个元素,因此数组越界异常。

于 2012-06-01T23:57:02.923 回答
1

在卡片中,您有:

public int nRank; // Used later
public int maxRank = 13; //The max number of Ranks
public int nSuit; // Used later
public int maxSuit = 4; // Max number of suits

然后后来

public String suit = suits[nSuit]; //The suit string of the card whose suit number is nSuit
public String rank = ranks[nRank]; //Same but with ranks

nSuit 和 nRank 没有被初始化为任何理智的东西。尝试设置为 0 之类的值。但是,我认为这实际上不是您想要的,因为套装和等级将在对象构造时设置,而不是您调用构造函数的结果。

于 2012-06-01T23:58:33.877 回答
1

当您尝试运行它时会发生该错误(而不是在您尝试编译它时)。这有点挑剔,但最好用正确的术语。

问题是您试图在数组中获取索引超出范围的元素。在您的数组声明(等级和西装)中,您使用max - 1,而它应该是max。例如,suits[3] = "Hearts"无效,因为suits它的长度仅为 3 ( maxSuit -1= 3)。

于 2012-06-01T23:58:55.967 回答