Question: Below is my code for java deck of cards program. The objective is to have one random card displayed which is the first card, and
Below is my code for java deck of cards program. The objective is to have one random card displayed which is the first card, and be able to display 52 cards. So for example the first card is displayed, then ok is pressed in GUI and then second card is displayed.. and so on, and user can get 52 random cards to show. If the user tries to get to 53 cards there should be an error message. (Right now the only thing that it's displaying is my int decknum which just says 52).
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Cards {
public static void main(String[] args)
{
int[] deck = new int[52];
String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10","Ace", "Jack", "Queen", "King" };
ArrayList
ArrayList
for (int i = 0; i < deck.length; i++)
deck[i] = i;
for (int i = 0; i < deck.length; i++)
{
int shuffle = (int) (Math.random() * deck.length);
while(randArr.contains(shuffle))
{
shuffle = (int) (Math.random() * deck.length);
}
randArr.add(shuffle);
shuffle (Math.random()*100 );
int temp = deck[i];
deck[i] = deck[shuffle];
deck[shuffle] = temp;
}
int decknum = 0;
for (int i = 0; i < 52; i++)
{
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
decknum++;
System.out.println("card number " + decknum+": "+rank + " of " + suit);
shuffledDeck.add(rank + "of" + suit);
}
for (int i = 0; i < 2; i++)
{
JOptionPane.showMessageDialog(null, decknum);
}
}
private static ArrayList
{
return null;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
