0

I need to use public static void PrintCard (int number, int suit) as the header in my program to print 5 random cards (number & suit) example "Ace of Spades".

I'm not entirely sure where to go about it, but I was wondering if there was a way to make a randomly generated number = Ace or etc., because Ace isn't a variable I get a error.

example: 0 = Ace (won't work because variable on wrong side) and

number == 0
return Ace;

those are things I've tried, sorry if its really broad or etc. I'm just pretty lost and novice when it comes to coding.

edit:

#  Card
_______
0  Ace 
1  Two
2  Three
3  Four
4  Five
5  Six
6  Seven
7  Eight
8  Nine
9  Ten
10 Jack
11 Queen
12 King

#  Suit
_______
0 Spaces
1 Hearts
2 Clubs
3 Diamonds

Structure of your program


import....

class {

  main method {
    num
    suit

    loop {
      loop 5x {
         num = generate random number 0-12
         suit = generate random number 0-3
         PrintCard (num, suit)
      }
      Prompt the user to continue
      read input from the user into a string
      if (input is yes)
         stay inside this loop, otherwise get out
     }
  }

  PrintCard (number, suit) {
    switch (number)
      // print the word for the card value
    print " of "
    switch (suit)
      // print the word for the card suit
  }

}
sampathsris
  • 21,564
  • 12
  • 71
  • 98

2 Answers2

1

You could put the face and suit names into String arrays, and then pick a random entry from each array. First, you'd need a way to pick an item at random from an array. Something like,

private static Random rand = new Random();
public static <T> T randomEntry(T[] arr) {
    return (arr != null) ? arr[rand.nextInt(arr.length)] : null;
}

Then you could generate card names and add them to a LinkedHashSet (to preserve insertion order) until you have 5 entries,

String[] names = { "Ace", "Deuce", "Trey", "Four", "Five", "Six",
        "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String[] suits = { "Spades", "Clubs", "Hearts", "Diamonds" };
Set<String> cards = new LinkedHashSet<>();
while (cards.size() < 5) {
    cards.add(String.format("%s of %s", randomEntry(names), 
        randomEntry(suits)));
}

Then your output is just

for (String card : cards) {
    System.out.println(card);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • mind if I ask a few questions to get a better idea, I'm not too familiar with arrays the first part that starts with "" what does that exactly do? edit woops, and how would I incorporate public static void PrintCard (int number, int suit) as my header, thanks by the way your comment and previous one gave me a better idea of what it should look it, just trying to trial and error right now and get a better grasp on the whole thing. – Nova Le League Oct 16 '14 at 06:47
  • I could rewrite it to use your `PrintCard` method, but that covers only printing like `String.format("%s of %s", randomEntry(names), randomEntry(suits))`; no tracking of used cards because your method is standalone. Basically, you'd be picking the card before you call `PrintCard` because it takes the number and the suit as arguments. `names[number]` and `suits[suit]`. – Elliott Frisch Oct 16 '14 at 13:31
0

Before you start to write code i suggest you to do some planning using classic pen and paper. There is several steps, that you can go through that will help you to simplify your work.
First, write down WHAT you want your program to have. In your example it can be like this:

  • Generating random suit of a card
  • Generating random number of a card
  • Storing generated cards
  • Storing generated cards in group of 5
  • Printing out the results

Next step after you did this is to ask and answer guestion HOW you can do things written in the list. For example:

  • How to generate random suit or number? Java has a class called Random. All the methods could be found here.

  • How to store generated card? Java has lot's of utilities to store sets of data. For example ArrayList. All the methods and info could be found here.

There are 13 cards in suit. There are 4 suits. So you must have two ArrayList to store all the naming of cards and suits.
You must generate 2 different integers: 0-12 and 0-3.
You must store generated card in another ArrayList so you can access it later.
Loop process, as far as you can do this there is nothing in printing out the results taken from Array.

UPD1 code example:
Here is example of a code you may need:

    //variables to store generated card as string

    private String generatedCard;
   //create Array list for suits
  ArrayList<String> suits = new ArrayList<>();
  //fill the List
  suits.add("spades"); //index no [0]
  suits.add("hearts");
  suits.add("diamonds");
  suits.add("clubs"); //index no [3]
  //create Array list for numbers
   ArrayList<String> numbers = new ArrayList<>(); 
  //fill the list
   - - - -
  //create ArrayList for generated Cards
   ArrayList<String> cards = new ArrayList<>();
  //create Randomiser
    Random rnd = new Random();
  for (int i = 0; i <5; i++){
        generatedCard = ""+suits.get( rnd.nextInt(4)) + " " + numbers.get(rnd.nextInt(13)); 
      cards.add(i, generatedCard);
  }   

For printing out use for loop with System.out.println(cards.get(indexer));

iColdBeZero
  • 255
  • 2
  • 11
  • thanks, that made a lot of sense. I put the numbers and suits in a group like this so far edit: pressed enter on accident; public class Cards2 { public static void PrintCard (int number, int suit){ String[] numbers = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}; String[] suits = {"Spades","Clubs","Hearts","Diamonds"}; } } – Nova Le League Oct 16 '14 at 07:03
  • sorry I haven't used arrays yet, but what do "<>" indicate? I also updated my post, to include a sort of pseudo code I was given, I was curious on if my starting method would be public static void PrintCard (int number, int suit) because the word "header" in the problem confused me or if it just wouldn't fit as my first method, because I've trying to plan out what methods I would use for each step of the code. Thanks again for the help already gave me a grasp on how to plan out everything. – Nova Le League Oct 16 '14 at 16:55
  • also, how would I add the index number, I see its in the comment, but I wasn't sure how I assigned it – Nova Le League Oct 16 '14 at 17:43
  • @NovaLeLeague `<>` indicates variable types, that array can accept. For exmaple to `ArrayList` can be added only integers and adding string to this will return error. But keep in mind that not only primitives can be stored in List, for example `ArrayList` can accept any element. About indexes, think about them like any other array(like dynamic array in C++), first element has index of [0], second [1] and fifteenth will be [14]. And if i helped you, don't forget to accept my answer. – iColdBeZero Oct 17 '14 at 03:49