2

私はJavaを学ぼうとしているかなり初心者で、自分で行ったタスクを完了するのに少し苦労しています。基本的に、私はこのページの最後にある演習を行おうとしています。

私はなんとか3つのクラスでそれを終えることができました。カード

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];   
}
}

そして、私がシャッフルと呼んだカード/デッキの表示者:

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

ランク[]とスーツ[]の両方のインデックスは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個の値を入力する場合は、newString[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。たとえば、は長さが3(= 3)しかないため、suits[3] = "Hearts"は無効です。suitsmaxSuit -1

于 2012-06-01T23:58:55.967 に答える