1

I want to display different string array elements like this:

  • Clubs 1
  • Hearts Ace
  • Diamonds 9

CardGame.java:

public class CardGame {
    public static void main(String[] args){
        String[] suit = { "Clubs", "Hearts", "Diamonds", "Spades" };
        String[] deck = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                        "Jack", "Queen", "King", "Aces" };

        int i = (int) ( Math.random() * deck.length );
        int j = (int) ( Math.random() * suit.length );


        for( int a = 0; a < 7; a++ ) {
            System.out.println( "Deck " + deck[i] + " Suit " + suit[j] );
        }

        System.out.println();
    }
}

How will I do it? Point me to correct logic of displaying those different elements. Thanks.

3
  • 2
    Well, look at where you're looping... and then look at where you're setting the values of i and j. How do you expect what you're printing to change if i and j don't change? (Also note that your arrays are misnamed. The suits are hearts, clubs, diamonds and spades... Commented Oct 9, 2012 at 8:29
  • You want to display all the possible combinations and in random order ? Commented Oct 9, 2012 at 8:30
  • @KaipaMSarma yup I want them random and the possible combinations. Commented Oct 9, 2012 at 8:37

6 Answers 6

3

put

int i = (int) ( Math.random() * deck.length );
int j = (int) ( Math.random() * suit.length );

into the for loop, so that in every iteration a new random card is generated

Sign up to request clarification or add additional context in comments.

Comments

2
    int times = 10; // Say you want it 10 times, in your code it's 7.

    for (int i = 0; i < times; i++) {
        System.out.println(deck[(int) (Math.random() * deck.length)]
                 + " : " + suit[(int) (Math.random() * suit.length)]);
    }

1 Comment

Good one liner ;) I salute you sir.
0
public class CardGame {
    public static void main(String[] args) {

         String[] deck = { "Clubs", "Hearts", "Diamonds", "Spades" };
         String[] suit = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "Jack", "Queen", "King", "Aces" };

         for( int a = 0; a < 7; a++ ) {
             int i = (int) ( Math.random() * deck.length );
             int j = (int) ( Math.random() * suit.length );

             System.out.println( "Deck " + deck[i] + " Suit " + suit[j] );
         }
         System.out.println();
    }
}

Comments

0
int i = (int) ( Math.random() * deck.length );
int j = (int) ( Math.random() * suit.length );

put into for loop because your new generate i j value get

Comments

0

I don't quite get your question - if you want many random cards, or all of them.

All of them:

for (String cardSuit: suit) {
    for (String cardDeck: deck) {
        System.out.println(cardDeck + " " + cardSuit);
    }
}

Random (given number):

Random rnd = new java.util.Random();
for (int i = 0; i < number; i++) {
    String cardSuit = suit[rnd.nextInt(suit.length)],
           cardDeck = deck[rnd.nextInt(deck.length)];
    System.out.println(cardDeck + " " + cardSuit);
}

1 Comment

Yeah, I've forgot to access the array. Fixed it.
0
import java.util.HashSet;
import java.util.Set;

public class CardGame { 

    public static void main(String[] args){ 

        String[] deck = { "Clubs", "Hearts", "Diamonds", "Spades" }; 
        String[] suit = { "2", "3", "4", "5", "6", "7", "8", "9", "10", 
                                    "Jack", "Queen", "King", "Aces" }; 

        Set<String> s = new HashSet<String>();
        int totalCards=0;

        while(s.size() < (deck.length * suit.length)){ 
            int i = (int) ( Math.random() * deck.length ); 
            int j = (int) ( Math.random() * suit.length ); 

            if(!s.contains("Deck " + deck[i] + " Suit " + suit[j])){
                s.add("Deck " + deck[i] + " Suit " + suit[j]);
                totalCards++;
            }

        }

        System.out.println("Cards at Random: ");
        for(String str: s)
        System.out.println(str); 

        System.out.println("Total Cards: "+ totalCards);
    } 
} 

This will return all 52 cards( all combinations) randomly

Comments

Your Answer

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