Javaの - どのように(静的な配列を中心とした)カードのデッキを作成するには? - APコンピュータサイエンスプロジェクト

user9125722:

これは今まで私の最初の質問です。ただ、明確にするために、私はこれを尋ねる前に、私を助けている可能性が何か質問があったかどうかを確認するためのチェックをしました。私は間違って何を行う場合は、事前に謝罪、私は新たなんです。

とにかく、私のAP CSクラスのために私はデッキにカードを作る必要がありますし、テキストウィンドウでそれをプリントアウト。私は、私は非常に近い仕上げにしています信じています。タイトルが言うように、一般的にどのように私は、静的な配列を中心としたデッキのカードを作成するのですか?(私はおよそ以下尋ねるエラーが解消された後)私がこれまでに生産されているコードを見ているときには、さらに、正しい方法でそれを行うことですか?

ここで与えられたコードは(これを変更することはできませんを意味する)です。

public class Card
{
   private String suit;
   private String rank;
   private int value;

   public Card(String s, String r, int v)
   {
      suit = s;
      rank = r;
      value = v;
   }

   public String getSuit()       { return suit; }
   public String getRank()       { return rank; }
   public int getValue()         { return value; }

   public void setSuit(String s) { suit = s; }
   public void setRank(String r) { rank = r; }
   public void setValue(int v)   { value = v; } 

   public String toString()
   {
      return "[" + suit + ", " + rank + ", " + value + "]";
   }
}

そして、ここで私がこれまでにコード化されたものです。

public class Lab11bst
{
    public static void main(String[] args)
    {
      Deck deck = new Deck();
      System.out.println(deck);
    }
}


class Deck
{
   private int numberOfCards;
   private Card [] cards;
   private String [] suits = {"Clubs","Diamonds","Hearts","Spades"};
   private String rank;
   private int value;


   public Deck() // This creates a deck of 52 playing cards.
   {
      numberOfCards = 52;
      cards = new Card[52];
      for ( int suit = 0; suit <= 3; suit++ )
      {
         String [] ranks = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
         for ( int rank = 1; rank <= 13; rank++ )
         {
            if (rank == 1)
            {
               this.rank = "Ace";
            }
            else if (rank == 11)
            {
               this.rank = "Jack";
            }
            else if (rank == 12)
            {
               this.rank = "Queen";
            }
            else if (rank == 13)
            {
               this.rank = "King";
            }
            else
            {
               this.rank = "" + ranks[rank];
            }
            for ( int value = 1; value <= 10; value++ )
            {
               if (this.rank == "Ace")
               {
                  value = 1;
               }
               else if (this.rank == "Jack")
               {
                  value = 10;
               }
               else if (this.rank == "Queen")
               {
                  value = 10;
               }
               else if (this.rank == "King")
               {
                  value = 10;
               }
               else
               {
                  this.value = value;
               }
               cards [numberOfCards] = new Card(suits[suit],this.rank,value);
               numberOfCards ++;
            }
         }
      }
   }
}

私のコードを見て、私はかなり確信して、すべてのif文ものと、それはすべてがより短く、簡潔に取り除くためのより良い方法があるんです。私のソートの3番目の質問(メイン二つの質問に答える前に解決するために必要な/のかもしれないの助けとなる場合があります)私はプログラムを実行すると、私はこのエラーを修正すればよい方法です?:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52
    at Deck.<init>(Lab11bst.java:89)
    at Lab11bst.main(Lab11bst.java:5)

事前にどうもありがとうございました。

本能:

私は、悲しいことに、特にして、いくつかの論理エラーを発見しnumberOfCards、あなたがのための指標として用いることをint型cardsの配列。その結果、私は最適化し、あなたを固定しているclass Deck、それが主な方法で動作し、class Cardあなたが作成しています:

class Deck {
    private int numberOfCards = 52;
    private Card[] cards;

    private String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
    String[] ranks = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen",
            "King" };
    int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };

    private String rank;
    private int value;

    public Deck() // This creates a deck of 52 playing cards.
    {
        cards = new Card[numberOfCards];
        int currentCardId = 0;
        for (int suit = 0; suit <= 3; suit++) {
            for (int rank = 0; rank <= 12; rank++) {
                this.rank = "" + ranks[rank];
                this.value = values[rank];

                cards[currentCardId] = new Card(suits[suit], this.rank, value);
                System.out.println(cards[currentCardId].toString()); //print out the cards .toString() as it was added to the array
                currentCardId++;
            }
        }
    }
}

それはいっぱいにCard[] cards、ゲッターとsystemoutを使用してそれをテストして自由に感じます。あなたが好きな情報とに従って配列を埋めることができますように、それは、cardnamingとvaluesettingに非常に適応性があります。あなたはさらなる説明をしたい場合は、私に知らせてください。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=176177&siteId=1
おすすめ