私は以下を達成しようとしていますが、シャッフルする前にデッキを印刷しようとしています。カードのデッキを印刷し、シャッフルして再印刷します。
Cardクラスの52個のオブジェクトを格納するDeckOfCardsというクラスを作成します。デッキをシャッフルしたり、カードを処理したり、デッキに残っているカードの数を報告する方法を含めます。シャッフル方法はフルデッキを想定する必要があります。シャッフルされたデッキから各カードを処理し、処理された各カードを印刷するメインメソッドでドライバクラスを作成します。
シャッフル前にデッキを処理する方法を理解できず、1枚のカードを扱い、いくつのカードがデッキにあるかを報告します。助けてください。
import java.util.Random;
public class DeckofCard {
private Cards[] cards;
int i;
DeckofCard()
{
i=52;
cards = new Cards[52];
int x=0;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards[x] = new Cards(a,b);
x++;
}
}
}
public Cards drawFromDeck()
{
Random generator = new Random();
int index=0;
do {
index = generator.nextInt(52);
} while (cards[index] == null);
i--;
Cards temp = cards[index];
cards[index]= null;
return temp;
}
public int getTotalCards()
{
return i;
}
}
は、ここに私のカードクラス
public class Cards
{
private int rank, suit;
private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
Cards(int suit, int rank)
{
this.rank=rank;
this.suit=suit;
}
public @Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
}
と私の販売店です。
public class Dealers {
public static void main(String[] args)
{
DeckofCard deck = new DeckofCard();
Cards C;
System.out.println(deck.getTotalCards());
while (deck.getTotalCards()!= 0)
{
C = deck.drawFromDeck();
System.out.println(C.toString());
}
}
}
これは宿題とよく似ていますが、これまでシャッフルの方法で試したことはありますか? – user1875195
それは宿題であり、私が試みたことは間違っていると思います。 – Savage99
ランダムジェネレーター=新しいランダム(); int index = 0; do { index = generator.nextInt(52); } while(cards [index] == null); i--; カードtemp = cards [index]; cards [index] = null; 戻り値。 – Savage99