我是一个相当努力学习Java的人,我在完成我给自己的任务时遇到了一些麻烦。基本上,我正在尝试在page.
结束时进行练习我成功完成了三节课。 The Card :
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;
}
}
The Deck:
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)
我错过了什么?
答案 0 :(得分:3)
在你所有长度为N的循环中,你试图从元素1..N中访问。你应该从0 ..(1-N)循环。
例如,如果您有4个套装,则您有一个元素为0,1,2,3的数组。通过说(for j=1; j<=4, j++)
,你试图访问超出界限的元素4。
应该阅读(j=0; j<4; j++)
。
答案 1 :(得分:2)
rank []和suit []的索引从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];
}
}
答案 2 :(得分:2)
Java数组基于零...所以创建一个13 - 1 == 12个元素的新数组,但是数组从零开始意味着访问元素“12”实际上是第13个元素,因此数组超出界限异常。
答案 3 :(得分: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之类的。但是,我不认为这实际上是你想要的,因为套装和等级将在对象构造时设置,而不是你对构造函数的调用。
答案 4 :(得分:1)
当您尝试运行它时发生错误(而不是在您尝试编译它时)。这有点挑剔,但最好让术语正确。
问题是你正在尝试使用超出范围的索引获取数组中的元素。在您的数组声明(rank和suit)中,您使用max - 1
,而它应该是max
。例如,suits[3] = "Hearts"
无效,因为suits
的长度仅为3(maxSuit -1
= 3)。