Results 1 to 8 of 8
- 10-11-2010, 06:06 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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");}
}
}
}
- 10-11-2010, 06:24 PM #2
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?
- 10-11-2010, 08:03 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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?
- 10-11-2010, 08:10 PM #4
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 == ...".
- 10-11-2010, 08:17 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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.
- 10-11-2010, 08:23 PM #6
That's the output I get from your program.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
- 10-12-2010, 04:41 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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!!!!
- 10-12-2010, 07:37 AM #8
Without arrays, that's probably the best you can do.
Glad you got it worked out. :)
Similar Threads
-
Need help with printing console output to JTextArea
By ShinTec in forum AWT / SwingReplies: 4Last Post: 06-04-2010, 10:10 AM -
Poker Simulation (shuffling a deck of cards?)
By StateofMind in forum New To JavaReplies: 4Last Post: 04-06-2010, 08:59 PM -
Deck of Cards
By khunmato in forum New To JavaReplies: 13Last Post: 09-06-2009, 05:47 PM -
Problem with making/sorting a deck of cards
By Franneldort in forum New To JavaReplies: 9Last Post: 11-07-2008, 12:47 AM -
creating a deck of cards using a linked list
By boomba88 in forum New To JavaReplies: 2Last Post: 09-11-2008, 03:34 PM


LinkBack URL
About LinkBacks

Bookmarks