• Post Reply Bookmark Topic Watch Topic
  • New Topic

Could someone explain how this basic deck of cards program works?  RSS feed

 
Bryce Hawking
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to understand this. I don't know what the for loop is doing, or what d[i] does. It doesn't make any sense to me, and I'd like to be able to understand it.



[Moderator edit: added code tags]
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

When posting code listing, please UseCodeTags (←click on that link to learn how). I have added them for you this time.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by reading the Java Tutorials about Arrays and the for-loop. When you've done that, come back here if you still have questions or doubts.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what these for-loops do:

1st for-loop: initializes array d so that it contains the numbers 0 - 19, in that order.

2nd for-loop: randomly "shuffles" the values that are in array d so that they are no longer in order.

3rd for-loop: translates each number in array d into an equivalent suit and rank and displays these.

As far as I can tell, this program will only ever display "spades" and "clubs" for the suit. If you changed line 6 to create an array with 52 elements instead, then you'll get a full deck of cards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!