Java - 如何将switch语句中的值插入到数组中

时间:2017-02-06 02:49:26

标签: java arrays

我正在尝试创建一个程序,可以从一副52张牌中取出两张牌。到目前为止,我已经使用switch语句将卡值分配给一个数字,例如,Ace of Spades = 0,Seven of Clubs = 26。但是,为了拥有两张牌,它们必须是唯一的,我不知道如何去做。

import java.lang.Object;
import java.util.Scanner;
import java.util.Random;

public class Deck {

public static void main(String[] args) {

    Random rnd = new Random();
    int cardNumber = rnd.nextInt(52);
    String toCard = null;
    switch (cardNumber){
    case 0: toCard = "Ace of Spades";
            break;
    case 1: toCard = "Ace of Hearts";
            break;      
    case 2: toCard = "Ace of Clubs";
            break;
    case 3: toCard = "Ace of Diamonds";
            break;      
    case 4: toCard = "Two of Spades";
            break;
    case 5: toCard = "Two of Hearts";
            break;      
    case 6: toCard = "Two of Clubs";
            break;      
    case 7: toCard = "Two of Diamonds";
            break;      
    case 8: toCard = "Three of Spades";
            break;      
    case 9: toCard = "Three of Hearts";
            break;      
    case 10: toCard = "Three of Clubs";
            break;      
    case 11: toCard = "Three of Diamonds";
            break;  
    case 12: toCard = "Four of Spades";
            break;      
    case 13: toCard = "Four of Hearts";
            break;      
    case 14: toCard = "Four of Clubs";
            break;      
    case 15: toCard = "Four of Diamonds";
            break;  
    case 16: toCard = "Five of Spades";
            break;      
    case 17: toCard = "Five of Hearts";
            break;      
    case 18: toCard = "Five of Clubs";
            break;      
    case 19: toCard = "Five of Diamonds";
            break;
    case 20: toCard = "Six of Spades";
            break;      
    case 21: toCard = "Six of Hearts";
            break;      
    case 22: toCard = "Six of Clubs";
            break;      
    case 23: toCard = "Six of Diamonds";
            break;
    case 24: toCard = "Seven of Spades";
            break;      
    case 25: toCard = "Seven of Hearts";
            break;      
    case 26: toCard = "Seven of Clubs";
            break;      
    case 27: toCard = "Seven of Diamonds";
            break;
    case 28: toCard = "Eight of Spades";
            break;      
    case 29: toCard = "Eight of Hearts";
            break;      
    case 30: toCard = "Eight of Clubs";
            break;      
    case 31: toCard = "Eight of Diamonds";
            break;
    case 32: toCard = "Nine of Spades";
            break;      
    case 33: toCard = "Nine of Hearts";
            break;      
    case 34: toCard = "Nine of Clubs";
            break;      
    case 35: toCard = "Nine of Diamonds";
            break;
    case 36: toCard = "Ten of Spades";
            break;      
    case 37: toCard = "Ten of Hearts";
            break;      
    case 38: toCard = "Ten of Clubs";
            break;      
    case 39: toCard = "Ten of Diamonds";
            break;
    case 40: toCard = "Jack of Spades";
            break;      
    case 41: toCard = "Jack of Hearts";
            break;      
    case 42: toCard = "Jack of Clubs";
            break;      
    case 43: toCard = "Jack of Diamonds";
            break;
    case 44: toCard = "Queen of Spades";
            break;      
    case 45: toCard = "Queen of Hearts";
            break;      
    case 46: toCard = "Queen of Clubs";
            break;      
    case 47: toCard = "Queen of Diamonds";
            break;
    case 48: toCard = "King of Spades";
            break;      
    case 49: toCard = "King of Hearts";
            break;      
    case 50: toCard = "King of Clubs";
            break;      
    case 51: toCard = "King of Diamonds";
            break;
    }


System.out.println(cardNumber + " " + toCard);

}

}

我知道可能有更多有效的方法可以解决这个问题,但我对编码起来相对较新。我想将每个案例分配给一个数组中的索引,以便我可以调用say cardArray [0]并返回黑桃王牌,并最终从cardArray中调用两个随机数,并让它们成为唯一的卡片。

感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

为了向玩家发放两张独特的牌,你的班级设计应该与此类似:

播放器应该有一系列卡片。

([0-9]{4}(?:[1-9][0-9]|[0-9][1-9]))

CardDealer 应该有一副牌和public class Player { private String[] cards; public String[] getCards() { return cards; } public void setCards(String[] cards) { this.cards = cards; } @Override public String toString() { return "Player [cards=" + Arrays.toString(cards) + "]"; } } 方法,可以向给定的玩家发牌。

deal(Player... players)

现在,如果你运行这样的测试:

public class CardDealer {
    private static Random rand;
    private static final String[] DECK = { "Ace of Spades",
            "Ace of Hearts",
            "Ace of Clubs",
            "Ace of Diamonds",
            "Two of Spades",
            "Two of Hearts",
            "Two of Clubs",
            "Two of Diamonds",
            "Three of Spades",
            //etc.
    };
    //Use a java.util.Random object to generate random numbers.
    public CardDealer(){
        rand = new Random();
    }
    /**
      * This method accepts variable number of players
      * and for each player, it picks up two unique cards
      * from the deck, creates an array of those two cards
      * and assigns that array to the players
      */
    public void deal(Player... players){
        for(Player eachPlayer : players){
            // Create an array of 2 cards for each player
            String[] cards = new String[2];
            // Pick first card randomly from deck.
            int cardIndex1 = rand.nextInt(52);
            String card1 = DECK[cardIndex1];

            // Pick second card randomly from deck
            int cardIndex2 = rand.nextInt(52);
            // If the second picked index is equal to first, pick again
            while(cardIndex1 == cardIndex2){
                cardIndex2 = rand.nextInt(52);
            }
            String card2 = DECK[cardIndex2];

            //Give each player his cards.
            cards[0] = card1;
            cards[1] = card2;

            eachPlayer.setCards(cards);
        }
    }
}

它应该产生如下结果:

        CardDealer dealer = new CardDealer();
        Player sam = new Player();
        Player joe = new Player();

        dealer.deal(sam,joe);

        System.out.println("Sam -> " + sam);
        System.out.println("Joe -> " + joe);

我希望这会让你有一些想法继续进行。

答案 1 :(得分:0)

使用switch大小为String

的数组,而不是使用52语句为名称分配数字。
String []cards = new String[52];

并将卡片的名称存储在此数组中。 例如

cards[0] = "Ace of Spades";

要从数组中获取任何卡,只需参考存储它的数组的索引号

cards[indexnumber];

例如

cards[0];

将返回Ace of Spades,因为它存储在数组的索引0中。

请注意,每张卡在阵列中都有唯一的索引号。

答案 2 :(得分:0)

您可以使用Set来确保您的值是唯一的:

public ArrayList<String> drawCards(int noOfCardsToDraw)
{
    //set up an array of strings that represent your cards
    //and can be indexed 0-51 i.e. 52 cards total.
    String[] deck = {
                  "Ace of spades",
                  "two of spades",
                  "three of spades",
                  "for of spades"
                  //etc
                    };
    //a set of integers ensures that you have no duplicate
    //card values
    Set<Integer> cardValues = new Hashset<Integer>();

    //pick your random card values and add them to the set,
    //if there happen to be two identical random numbers
    //then inserting it into the set does nothing, so this
    //will loop until your chosen number of cards have been
    //picked.
    while(result.size < noOfCardsToDraw)
    {
        cardValues.add(deck[rnd.nextInt(51)]);
    }

    //and arraylist of strings to hold the card designations
    ArrayList<String> result = new ArrayList<String>();

    //loop through the set of card values adding the corresponding
    //string at that index to the arraylist.
    for(int card : cardValues)
    {
     result.add(deck[card]);
    }

    //return the arraylist to do with as you please
    return result;
}

这应该继续挑选随机卡,直到该集与您传入方法的数量相同。所以,如果你这样称呼它:

ArrayList<String> cards = drawCards(2);

然后您可以使用以下方法检索值:

cards.get(0) //for first element
cards.get(1) //for second element

然后你应该有两张独特的随机卡。

答案 3 :(得分:0)

static final String[] cards = {"Ace","Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
static final String[] colors ={"Spades", "Hearts", "Clubs", "Diamonds"};

public static String getAcard(int num) {
    String card = cards[num%cards.length];
    String color = colors[num/cards.length];
    return card + " of " + color;
}

public static void main(String[] args) {

    int deckSize = cards.length * colors.length;

    List<String> deck = new ArrayList<>();

    for (int i=0; i<deckSize; i++) {
        deck.add(getAcard(i));
    }

    Collections.shuffle(deck);
    List<String> pair = deck.subList(0, 2);
    pair.forEach( p-> System.out.println(p));

}