If you really don't want to use collections, you'll have to somehow keep track of which cards you've dealt to avoid duplicates. about the only way i can think of is make and arrayList of available cards. pick one at random, and put it in the first position (so you'll need another arrayList here). Then, remove the element selected from the first list, and chose an element from the remaining cards (note that you now have one less to chose from, so you need to pick an element between 0 and 50 now, instead of 0-51).
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Oct 06, 2004
this is what i have now
after the first loop runs, i have 4 of every card..but in the second loop somehow im getting duplicates. and card suit is just takin the remainder if its 0 and then that would be one suit, 1, 2, 3 would be the four suits.
Now you do the Java code. [ November 15, 2004: Message edited by: Mike Gershman ]
SCJP 1.4, SCWCD in process
Joined: Aug 07, 2003
Originally posted by Kenshin Himura:
Why do you have a do-while loop here? Line 2 ensures that every card element is initialized to a value between 1 and 13, so why loop? I read this as "Initialize the card value; then if a randomly chosen number j happens to equal the current card number, initialize it again to the same value."
Now, the reason you're still seeing duplicates is that you're printing out the cards as you shuffle them. Thus, you aren't guaranteed to print each card, and you will likely print the same card more than once.
Pull out the println() from the last loop and make a third loop over all the cards to print them -- after shuffling. If you want to store the card suits and have them stick to the cards as they're shuffled, you'll need dto either create a Card class or use another array. If the latter, initialize it when initializing the cards and shuffle it along with the card array.
Joined: Oct 06, 2004
Thanks for anyone who posted!
I really liked how david explained it though, and it was pretty easy. I think im starting to understand arrays better.