Results 1 to 8 of 8
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Creating a deck of cards and printing in console

    Hello,

    I'm a newbie and read quite a bit of a java book and also taking a course but I think I'm a little slow in learning. What my assignment is to create a deck of cards and print it out in console. I created this way to output the first 26 cards in the console, but i'm sure that this is not what i'm supposed to do as the program is pretty long. Take a look please and if you can point me in the right direction. tks

    public class DeckofCards {


    public void outputNumbers() {

    for(int i = 1; i <= 13; i++) {
    if(i <= 10){ System.out.println(i + " of Clubs");}
    if(i == 11) { System.out.println("Jack of Clubs");}
    if (i == 12) { System.out.println("Queen of Clubs");}
    if (i == 13) { System.out.println("King of Clubs");}
    }
    for(int j = 1; j <= 13; j++) {
    if(j <= 10){ System.out.println(j + " of Diamonds");}
    if(j == 11) { System.out.println("Jack of Diamonds");}
    if (j == 12) { System.out.println("Queen of Diamonds");}
    if (j == 13) { System.out.println("King of Diamonds");}

    }
    }

    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,984
    Rep Power
    9

    Default

    Seems reasonable to me. Instead of having 4 different loops though, you could put a single 1-13 (or 0-12) loop inside another loop that loops from 1-4 (or 0-3), and use the value of that outer loop to determine which suit you're currently printing?

  3. #3
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    Thanks for the reply, I redid the code and now am stuck on declaring the Jack, Queen, King, Ace. I'm posting the code beolow but have no idea how to change the 1 to ace, 11 to jack.....

    public class DeckofCards {

    public static String clubs = "clubs";
    public static String diamonds = "diamonds";
    public static String spades = "spades";
    public static String hearts = "hearts";


    public void outputNumbers() {


    for(int i = 1; i <= 4; i++)
    {
    for(int j = 1; j <= 13; j++)
    if(i == 1)
    System.out.println(j + " of " + clubs);
    else if (i == 2)
    System.out.println(j + " of " + diamonds);
    else if (i == 3)
    System.out.println(j + " of " + spades);
    else if (i == 4)
    System.out.println(j + " of " + hearts);
    }
    }
    }

    Any ideas?

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Instead of doing the "if i ==", you could create an array to store the values then reference yourArray[i] to call it. For the Ace/Jack/etc., just do what you did before with the "if j == ...".

  5. #5
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    We have not learned arrays yet so I'm guessing I shouldn't be using it. My other issue is that when i run the program it starts with a 6 of spades, but i want it to start with 1 of spades.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Java Code:
    run:
    1 of clubs
    2 of clubs
    3 of clubs
    4 of clubs
    5 of clubs
    6 of clubs
    7 of clubs
    8 of clubs
    9 of clubs
    10 of clubs
    11 of clubs
    12 of clubs
    13 of clubs
    1 of diamonds
    2 of diamonds
    3 of diamonds
    4 of diamonds
    5 of diamonds
    6 of diamonds
    7 of diamonds
    8 of diamonds
    9 of diamonds
    10 of diamonds
    11 of diamonds
    12 of diamonds
    13 of diamonds
    1 of spades
    2 of spades
    3 of spades
    4 of spades
    5 of spades
    6 of spades
    7 of spades
    8 of spades
    9 of spades
    10 of spades
    11 of spades
    12 of spades
    13 of spades
    1 of hearts
    2 of hearts
    3 of hearts
    4 of hearts
    5 of hearts
    6 of hearts
    7 of hearts
    8 of hearts
    9 of hearts
    10 of hearts
    11 of hearts
    12 of hearts
    13 of hearts
    That's the output I get from your program.

  7. #7
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    ok. cool. might not be the most efficient way but i figured it out. My final code is......


    import java.lang.Integer;

    public class DeckofCards {

    public String face;
    public String suit;



    public void outputNumbers() {


    for(int i = 1; i <= 4; i++)
    {
    if(i==1) suit = "Clubs";
    else if (i==2) suit = "Diamonds";
    else if (i==3) suit = "Spades";
    else suit = "Hearts";
    for(int j = 1; j <= 13; j++)
    {
    if(j == 1) face = "Ace";
    else if(j==11) face = "Jack";
    else if (j==12) face = "Queen";
    else if (j==13) face = "King";
    else face = Integer.toString(j);
    System.out.println(face + " of " + suit);

    }
    }
    }
    }


    Thanks for the help everyone!!!!

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Without arrays, that's probably the best you can do.

    Glad you got it worked out. :)

Similar Threads

  1. Need help with printing console output to JTextArea
    By ShinTec in forum AWT / Swing
    Replies: 4
    Last Post: 06-04-2010, 10:10 AM
  2. Poker Simulation (shuffling a deck of cards?)
    By StateofMind in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 08:59 PM
  3. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 05:47 PM
  4. Problem with making/sorting a deck of cards
    By Franneldort in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 12:47 AM
  5. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •