I need to create a loop with all the possible combinations of cards and suits without any card being repeated. For example, out of 52 cards, there is only 1 possible combination that's 13 hearts 13 clubs 13 diamonds 13 spades. How do I generate this for all possible combinations?

I tried creating for loops:

int[] deck = new int[number];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
//initialize the cards
int i1 = 0, i2 = 0, i3 = 0, i4 = 0;
for(i1 = 0; i1< deck.length; i1++) {
    for(i2 = i1 + 1; i2< deck.length; i2++) {
        for(i3 = i2 + 1; i3< deck.length; i3++) {
            for(i4 = i3 + 1; i4< deck.length; i4++) {

                int spades = deck[i1];
                int hearts = deck[i2];
                int dimonds = deck[i3];
                int clubs = deck[i4];
                int total = spades + hearts + dimonds + clubs;
                //String s = spades + hearts + dimonds + clubs;
                if (total == number) {
                    if(i1 < 13)
                        System.out.println(spades +" "+suits[i1]+" " + hearts + suits[i2] + " " + dimonds + suits[i3] + " " + clubs + suits[i4]);

but I don't think this works.

import java.util.*;

public class Deck {

    public static void main(String[] args){

        //initialize everything
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();    
        int number = 0;
        System.out.print("Enter the sum of the combinations that you want: ");
        while (!scanner.hasNextInt()) {
              scanner.nextLine();
              System.out.print("Enter the sum of the combinations that you want: ");
        }
        number = scanner.nextInt();
        scanner.nextLine();
        while (number < 0) {
        }

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


        //initialize the cards
        for (int i = 0; i <deck.length; i++) {
        deck[i] = ranks[i%13] +" of "+ suits[i/13];
        System.out.println(deck[i]  +"\n");    
        }


    } //end main

} // end class    

Enter the sum of the combinations that you want:52 Here is the list of all the combinations:13 13 13 13 Total number of combinations: 270725 The number of picks that yields the sum of 52 is 1 Here is the list of all the combinations:12 13 13 13 12 13 13 13, 12 13 13 13, 12 13 13 13, 13 12 13 13, 13 12 13 13, 13 12 13 13, 13 13 12 13, 13 13 12 13, 13 13 13 12, 13 13 12 13, 13 12 13 13, 13 13 12 13, 12 13 13 13, 13 12 13 13, 13 13 12 13, Total number of combinations: 270725 The number of picks that yields the sum of 51 is 16

0 Answers