简体   繁体   English

将数组值列表分配为字符串

[英]Assigning a list of array values as a string

I have a program that generates a deck of playing cards and displays seven random cards. 我有一个程序,可生成一副扑克牌并显示7张随机纸牌。 I tried to pass the seven cards that were selected into a string, however I only know how to set the entire array to a string, not just the selected 7 cards. 我试图将选择的7张卡传递到字符串中,但是我只知道如何将整个数组设置为字符串,而不仅是选择的7张卡。

public class PlayedCards{        
      public static void main(String[] args){     
        int[] deck = new int[52];     
        String[] suits = {"Spades", "Clubs", "Diamonds", "Hearts"};    
        String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};    
        for (int a = 0; a < deck.length; a++){
                deck[a]= a;
        }

        for (int a = 0; a <deck.length; a++){
            int order = (int)(Math.random() * deck.length);
            int temp = deck[a];
            deck[a] = deck[order];
            deck[order] = temp;
        }

        for (int a = 0; a < 7; a++){
            String suit = suits[deck[a] / 13];
            String rank = ranks[deck[a] % 13];
            System.out.println(rank + " of " + suit);
        }

        System.out.println(rank + " of " + suit);
     }
}

My questions are: 我的问题是:

Are the cards that are displayed the values of 0-6 in my array? 在我的阵列中显示的卡的值是否为0-6?

If so, how would I set those values for the array to a separate string that i could then recall for the user as being played? 如果是这样,我将如何将数组的这些值设置为一个单独的字符串,然后我可以在播放该字符串时为用户调用该字符串?

If you just want to create a String that represents what was played for the player, either directly concatenate each card type and number to a resulting String (implicitly creating a new String on each concatenation) or, better yet, use a StringBuilder to do that and convert to a String at the end. 如果您只想创建一个表示玩家所玩游戏的String ,则可以直接将每种卡类型和数字连接到生成的String (在每个串联中隐式创建一个新的String ),或者更好的是,使用StringBuilder来实现并在最后将其转换为String So like this: 像这样:

StringBuilder sb = new StringBuilder();
for (int a = 0; a < 7; a++) {
...
sb.append(a+1).append(": ").append(rank).append(" of ").append(suit).append("\n");
...
String playedCards = sb.toString();
System.out.println(playedCards);

Better create a immutable card class 更好地创建一个不变的卡类

class Card
{
     private final int m_suit;
     private final int m_rank;

     public Card( int suit, int rank )
     {
         m_suit = suit;
         m_rank = rank;
     }

     public int getSuit()
     {
          return m_suit;
     }

     public int getRank()
     {
          return m_rank;
     }

}

Now you can create the card instance and pass it around or store it in a ArrayList. 现在,您可以创建卡实例并将其传递或存储在ArrayList中。 For eg 例如

Card card1 = new Card( deck[a] / 13, deck[a] % 13);
System.out.println( rank[card1.getRank()] + " of " + suit[card1.getSuit()]);
public class PlayCards {
public static void main(String[] args) {
    int[] deck = new int[52];
    String[] suits = { "Spades", "Clubs", "Diamonds", "Hearts" };
    String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King" };
    for (int a = 0; a < deck.length; a++) {
        deck[a] = a;
    }

    for (int a = 0; a < deck.length; a++) {
        int order = (int) (Math.random() * deck.length);
        int temp = deck[a];
        deck[a] = deck[order];
        deck[order] = temp;
    }

    Card card = null;
    Card[] cards = new Card[7];
    for (int a = 0; a < 7; a++) {
        card = new Card(suits[deck[a] / 13], ranks[deck[a] % 13]);
        cards[a] = card;
    }

    Player player = new Player();
    player.showCards(cards);
}
}

class Card {
private String suit;
private String rank;

public Card(String suit, String rank) {
    this.suit = suit;
    this.rank = rank;
}

public String toString() {
    StringBuilder card = new StringBuilder();
    card.append(this.rank);
    card.append(',');
    card.append(this.suit);
    return card.toString();
}
}

class Player {
public void showCards(Card[] cards) {
    for (int a = 0; a < 7; a++) {
        System.out.println(cards[a].toString());
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM