首页 > 解决方案 > 如何计算数组中的特定字符 - Java

问题描述

我对java很陌生,这是我在这里的第一个问题,所以请多多包涵。

我有以下输出

电流输出

ST = Spade 10,HK = Heart King,C7 = Club 7 等的值代表什么。

上面的输出被洗牌成4个“手”并按降序排列。S - H - D - C。

我正在尝试计算每手牌中有多少黑桃、红心、方块和梅花,并分别显示它们,因此最终输出将如下所示。

最终输出.

我现在的代码是这样的。我被卡住了,甚至不知道如何开始。对任何错误或之前是否有人问过这个问题表示歉意

主班

private static void displayStringArray(String[] cardArray) 
{
    System.out.printf("Printing from Array %n");
    for (int i = 0; i<MAXD; i++)
    {
        if (i%MAXC == 0 && i != 0)
        {
            System.out.print(System.lineSeparator());
        }
            System.out.printf(cardArray[i].toString() + " ");

    }
    System.out.printf("%n---------------------------------------%n");
}

private static void transfer2D(String[][] twoD, String[] strArray)
{

        for (int j = 0; j < 4; j++) 
        {
            for (int k = 0; k < 13; k++)
            {
                twoD[j][k] = strArray[((j)*MAXC)+k].toString();
            }

        }
}

private static void sort2D(String[][] twoD)
{
    String[] row0 = twoD[0];
    String[] row1 = twoD[1];
    String[] row2 = twoD[2];
    String[] row3 = twoD[3];

    Arrays.sort(row0, Collections.reverseOrder());
    Arrays.sort(row1, Collections.reverseOrder());
    Arrays.sort(row2, Collections.reverseOrder());
    Arrays.sort(row3, Collections.reverseOrder());

}

private static void print2D(String[][] strArray)
{   
    System.out.println("Re-arrange the cards");
    for (int i = 0; i<4; i++)
    {   
        System.out.print(System.lineSeparator());
        for (int j = 0; j<13; j++)
        {
            System.out.printf(strArray[i][j].toString() + " "); 
        }
    }
}

private static void getHandInfo(String[] anHand, MyInt s, MyInt h, MyInt d, MyInt c)
{
    for (int i = 0; i<MAXD; i++)
    {
        if (i%MAXC == 0 && i != 0)  
        {
            System.out.print(System.lineSeparator());
        }
        System.out.print(anHand[i].toString() + " ");

    }

    System.out.print(s + " - " +h+ " - " +d+ " - " +c+ " - ");
}

public static void main(String[] args) 
{
    ArrayList<PlayingCard> cards = new ArrayList<PlayingCard>();
    PlayingCard[] taCards = new PlayingCard[MAXD];
    String[] sCards = new String[MAXD];
    String[][] twodCards = new  String[4][13];
    MyInt int1 = new MyInt();
    MyInt int2 = new MyInt();
    MyInt int3 = new MyInt();
    MyInt int4 = new MyInt();

    deckOfCards(cards);
    printDeck(cards);

    listToArray(cards, taCards);
    shuffle(taCards);
    transfer(taCards, sCards);

    printDeck(taCards);

    transfer2D(twodCards, sCards);
    sort2D(twodCards);
    print2D(twodCards);

    //getHandInfo(sCards, int1 , int2, int3, int4);
}

MyInt 类

class MyInt
{
    private int n;

    public void setInt(int n)
    {
        this.n = n;
    }

    int getInt()
    {
        return n;
    }
}

编辑:当前输出

Re-arrange the cards

ST SQ SK SJ S9 S8 S7 S5 S4 S3 S2 CT CK
HQ HK HA H9 H8 H6 H5 H4 H3 H2 DT DA C4
HT H7 DQ DK DJ D9 D8 D7 D6 D5 D4 D3 D2
SA S6 HJ CQ CJ CA C9 C8 C7 C6 C5 C3 C2

最终输出达到

Re-arrange the cards

SA S9 S4 S3 HJ H4 DA DT D9 D7 D3 CK C4
4 - 2 - 5 - 2
SK ST S6 HQ H5 H2 DJ D2 CA CQ C8 C5 C2
3 - 3 - 2 - 5
SQ S8 S7 HA HT H9 DQ D5 D4 CT CJ C6 C3
3 - 3 - 3 - 4
SJ S5 S2 HK H8 H7 H6 H3 DK D8 D6 C9 C7
3 - 5 - 3 - 2

标签: javaarraysarraylist

解决方案


请注意,以下解决方案是一个演示方法,它只是向您展示如何计算卡片。它不关心重复或排序,而是计算特定花色中正确数量的牌。main 的第一部分生成卡片,所以忽略它。看注释后的代码//this is the basis for your method。我已经用 switch 语句解决了它,但可能有更优雅的方法来解决它。

public class HelloWorld{

     public static void main(String []args){
         //this method generates random cards.
         //I dont worry about doubles for now.
        String [][] cards = new String[4][13];
        String[] suits = {"S","H","D","C"};
        int[] numbers = {1,2,3,4,5,6,7,8,9,10};
        for(int i = 0; i<4; i++) {
            for (int j = 0; j<13; j++) {
                String suit = suits[(int) Math.floor(Math.random()*4)];
                int number = numbers[(int) Math.floor(Math.random()*10)];
                String card = suit+number;
                cards[i][j] = card;
            }
        }
        //prints the first hand.
        //Note it is shuffled, but that doesn't matter
        for(int i = 0; i<13; i++) {
            System.out.print(cards[0][i]+", ");
        }

        //this is the basis for your method
        int[]counts = new int[4];
        for(int i = 0; i<13; i++) {
            switch(cards[0][i].charAt(0)) {
                case 'S': counts[0]++; break;
                case 'H': counts[1]++; break;
                case 'D': counts[2]++; break;
                case 'C': counts[3]++; break;
            }
        }
        System.out.println();
        for(int i = 0; i<4; i++) {
            System.out.print(suits[i] + ": " + counts[i] + ", ");
        }


    }
}

并输出:

H4, S1, S10, C2, S5, C10, S8, S1, H8, H6, C5, D1, C3, 
S: 5, H: 3, D: 1, C: 4, 

推荐阅读