Print all cards in the deck (Java)

Asked

Viewed 171 times

-7

Hello.

I am trying to create a program that prints all 52 cards in the deck, from 1 to 10 and with all suits (Clubs, Diamonds, Hearts, Spades) and also print the cards Jack, Queen and King.

I found the use of "for" more appropriate in this case, but I do not have much knowledge with repetition functions, I would like to know how to print a "String" using "for"?

I am using this logic but it is not practical, because I would have to create a "for" for each number, I need a logic that simplifies this.

    for (int i = 1; i <= 10; ++i) {
        System.out.println(i  +  " De Paus");
    }

    }
}

1 answer

1


I don’t know if it’s time to copy here, but there’s a lot of clasps there.

I would recommend studying arrays, think of them as a set of variables, instead of having an int value on the variable you have an array (set) of 13 ints within that array.

You can have an array of suits and then use the for to do what you did, or an array of suits, each item being another array for the cards.

Then you can scroll through this array (we call iterate in the array) to print.

Here is a link with an initial reference for you based on Devmidia

  • Okay, thank you ! I’ll give a study..

  • @Césaraugusto succeeds and does not forget to accept the answer as correct, it is important for other people who may have similar doubts in the future

  • I’ll do it. But one question, in this link he is creating the Array with "int", if I want to create an Array of suits I would not have to save as "String" ?

  • Arrays can be of the type you want, it can be an int array, String, if you want an Array of Arrays, this is what they call an Array

  • I understood, in the case of a String array this would be the correct way to declare ? String[] naipesCartas = {"Hearts,Diamonds,Spades,Clubs"};

  • A String is bounded by quotation marks, so the correct is to use String[] naipesCartas = {"Copas","Ouros","Espadas","Paus"}, so you have 4 strings being passed to your array, in your example there was only one string, with the content being the name of the 4 suits.

  • When writing code use a ` before and after the code to make it more readable.

  • Perfect ! I’m managing to create a logic based on the material you sent me, not so hard, thanks for the help !

  • You’re welcome, the community is here for this, helping those who are starting, any questions feel at home here at Stack Overflow

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.