これに似た質問がたくさんありますが、まだ学んでいないコードで答えられています。私は配列リストなしでこのタスクを実行できるはずなので、それは私に挑戦を提供しています。戦争のゲームをプレイする必要があります。カードクラスを作成し、デッキを作成してから、最初のカードをランダムに生成し、デッキからカードを「削除」して2番目のカードを作成し、どのカードが勝ったかを確認します。この比較を次のように続けます。デッキが使い果たされました。これが私がこれまでに持っているものです:
public class Card
{
private int cardValue;
private String cardSuit;
private String stringValue;
static final String[] SUIT = {"Clubs", "Diamonds", "Hearts", "Spades"};
static final String[] VALUE = {"Ace","Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
/**
*CONSTRUCTOR for Card class
*contains the arrays for both suit and values
**/
public Card (int cardS, int cardV)
{
cardSuit = SUIT[cardS];
stringValue = VALUE[cardV];
}
/**
*Get method for card suit, access the value of cardSuit
**/
public String getCardSuit()
{
return cardSuit;
}
/**
*get method for card's VALUES
**/
public String getValue()
{
return stringValue;
}
//in order to display string value of cards
public String toString()
{
return String.format("%s of %s", stringValue, cardSuit);
}
}
public class Deck
{
private final int HIGH_SUIT=3;
private final int HIGH_VALUE=12;
int i = 0;
int cardCount;
Card[] fullDeck = new Card[52];
public Deck()
{
for(int s = 0; s <= HIGH_SUIT; s++)
{
//for loop to determine the suit
for (int v = 0; v <= HIGH_VALUE; v++)
{
//construct all 52 cards and print them out
fullDeck[i] = new Card(s, v);
cardCount = (i + 1);
i++;//increment the card counter
}
}
}
}
public class War3
{
public static void main(String[] args)
{
int i=0;
Deck[] fullDeck = new Deck[52]; //set up the deck
//create a random value for the first card
int r = ((int) (Math.random() * 100) % 51);//create random number
Card[] playerCard = new Card[r];//create the player's card
System.out.println("The card is: " + playerCard.toString());
}
ご覧のとおり、最初のカードの表示方法がわからないため、戦争3ではそれほど遠くまでは行きませんでした。実行すると、次のように表示されます。カードは次のとおりです。[LCard; @ 4a5ab2random#1アレイの最初のカードを表示するにはどうすればよいですか?最初のカードと2番目のカードを割り当て、両方を表示して比較する方法を理解するのに助けが必要です。まだまだ長い道のりなので、一歩ずつ進んでいきます。