我在尝试创建“shuffleDeck()”方法时遇到了困难。
我要做的是创建一个方法,该方法将采用数组参数(将是一副牌)洗牌,并返回洗牌后的数组列表。
这是代码:
class Card
{
int value;
String suit;
String name;
public String toString()
{
return (name + " of " + suit);
}
}
public class PickACard
{
public static void main( String[] args)
{
Card[] deck = buildDeck();
// display Deck(deck);
int chosen = (int)(Math.random()* deck.length);
Card picked = deck[chosen];
System.out.println("You picked a " + picked + " out of the deck.");
System.out.println("In Blackjack your card is worth " + picked.value + " points.");
}
public static Card[] buildDeck()
{
String[] suits = {"clubs", "diamonds", "hearts", "spades" };
String[] names = {"ZERO", "ONE", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "Jack", "Queen", "King", "Ace" };
int i = 0;
Card[] deck = new Card[52];
for ( String s: suits )
{
for ( int v = 2; v<=14; v++)
{
Card c = new Card();
c.suit = s;
c.name = names[v];
if ( v == 14)
c.value = 11;
else if ( v>10)
c.value = 10;
else
c.value = v;
deck[i] = c;
i++;
}
}
return deck;
}
public static String[] shuffleDeck( Card[] deck)
{
/** I have attempted to get two index numbers, and swap them.
I tried to figure out how to loop this so it kind of simulates "shuffling".
*/
}
public static void displayDeck( Card[] deck)
{
for ( Card c: deck)
{
System.out.println(c.value + "\t" + c);
}
}
}