简体   繁体   中英

How will i invoke the array parameters in java?

public void createDeck() {
    k=0;
    cards=new Card[52];

    for (i=0;i<4;i++) {
        for (j=0;j<13;j++){
            cards[k]=new Card(i,j);
            k++;                
        }
    }
}

public void shuffleDeck(int m){
    Random random = new Random(); 
    Card cardTemp;
    for(i=0;i<m;i++){
        k=random.nextInt(52);
        cardTemp=cards[i%52];
        cards[i%52]=cards[(i+k)%52];
        cards[(i+k)%52]=cardTemp;
    }                   
}

Hello. I want to take i and j which is in

cards[k]=new Card(i,j);

Because i want to show rank an suit by taking this i and j:

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

  private final static String[] suits = { "Hearts", "Spades", "Diamonds", "Clubs" };

   private final static String[] ranks  = { "Ace", "2", "3", "4", "5", "6",
       "7", "8", "9", "10", "Jack", "Queen", "King" };

In another class,

deck=new Deck();
deck.createDeck();
deck.shuffleDeck(111);

Should i use another array? I am really confused. I want to give the screen, for example ace hearts after shuffling.

it is not clear what are you asking, but i can guess that you want is when you draw a card from the shuffled deck you'll be able to see it's rank and suit. right?

then the answer is to add 2 methods to Card class:

public int getSuit()
{
   return suit;
}

public int getRank()
{
   return rank;
}

and then, when you draw a card:

Card c = cards[k];
int cSuit = c.getSuit();
int cRank = c.getRank();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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