1

I need to access the two String variables suit and rank, then use them outside of the for loop. Is this at all possible?

public class Deck {    

    public static void main(String[] args) {

        int deck[] = new int[52];
        String suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String ranks[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

        for(int i = 0; i < deck.length; i++) deck[i] = i;

        for(int i = 0; i < deck.length; i++) {
            int index = (int)(Math.random() * deck.length);
            int temp = deck[i];
            deck[i] = deck[index];
            deck[index] = temp;
        }
        // randomizes the deck
        for(int i = 1; i < deck.length; i++) {
            String suit = suits[deck[i] / 13];  // I want to access these two Strings
            String rank = ranks[deck[i] % 13];  // here
            System.out.println(rank + "\t" + suit);

        }
        // and use them here
        // I'm trying to make a poker game but need to use those two to deal a hand
    }
}
1
  • 2
    What is the purpose of that? then suit or rank will hold only last value. Commented Dec 30, 2013 at 8:52

2 Answers 2

3

If you're trying to deal a hand then it seems to me you need a collection - after all, a player holds multiple cards.

I think you need to take a step back, rather than continuing with your current code. I'd do it something like this:

  • Define one enum for suits
  • Define one enum for ranks
  • Create a Card class which has a suit and a rank
  • Create a Deck class to store "cards remaining in the deck", starting with one of each card (or more)
  • Create a Player class which has a collection of cards as the hand
  • Create a PokerGame class (or something similar) as the entry point - that's the class which has main in. (It doesn't make much sense for it to be in Deck, in my opinion.)
  • Deal from the deck to the player, as many cards as you want.
Sign up to request clarification or add additional context in comments.

3 Comments

Ya that's true. Sorry if it was a dumb question. I've only been trying to teach myself this week. Thank you for the help.
@user3136315: Not that dumb a question really - but it's worth learning the lesson that as soon as things start to be a bit odd, it's worth taking a step back and checking that what you're trying to achieve makes sense. I've often spent ages winning a small battle against code, only to find that I've lost the war by designing it badly to start with!
I've been trying to resolve this for about 2 hours now. Also I don't quite know what an enum is yet. Is that another variable type?
1

Define the variables outside of the foor-loop, like here:

String suit;
String rank;
for(int i = 1; i < deck.length; i++) {
    suit = suits[deck[i] / 13];  // I want to access these two Strings
    rank = ranks[deck[i] % 13];  // here
    System.out.println(rank + "\t" + suit);

    }
    // and use them here

    // I'm trying to make a poker game but need to use those two to deal a hand
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.