Linked Questions

Popular Questions

How to display a enum value using a user-entered int?

Asked by At

I need to do 3 things with the CardsDriver: using the default constructor Card() display a default card. Then, take a user's int and display a Card object using the parameterized constructor Card(int n) (Using mod%). Finally use showDeck() to display all 52 cards. (As you can tell I kinda did this using the enhanced for loops at the end, it works but I do not think it is the best way.)

Would really like some help with at least the second problem. I just need to understand what I need to implement..

package cards;

import java.util.Scanner;

public class CardsDriver
{

private static Scanner keyboard = new Scanner(System.in);

public static void main (String[] args)
{
    boolean another;
    int n;

    Card card = new Card();

    do
    {
        System.out.println("Type a non-negative integer. Type -1 to 
                                                                    stop.");
        n = keyboard.nextInt( );


        if (n == -1)
        {
            another = false;
        }
        else
        {
            another = true;
        }

    } while (another == true);

    System.out.println("All 52 Cards Follow:");
    showDeck( );
}

private static int getNextInt()
{
    return keyboard.nextInt();
}

private static void showDeck()
{
    for (Face face : Face.values())
    {
        for (Suit suit : Suit.values())
        System.out.println("The " + face + " of " + suit);
    }
}
}

and here is my Card class

package cards;

public class Card {
private Face face;
private Suit suit;

public Card() {
    face = Face.ACE;
    suit = Suit.CLUBS;
}

public Card(Card existingCard) {
    this.face = existingCard.face;
    this.suit = existingCard.suit;
}

public Card(int n) {
    face = Face.values()[n % 13];
    suit = Suit.values()[n % 4];
}

public String toString() {

    return "the" + face + "of" + suit;
}
}

Related Questions