问题描述:

I'm trying to make a simple text base blackjack game. I was wondering how I would go about putting a string inside of an array, or having a String array and assign them values outside the array. if that makes sense. below is the code that I have for it;

int deck[] = {2,3,4,5,6,7,8,9,10,Jack,Queen,King,Ace};

obviously this won't work because the array is for an integer. Possibly a string array then assign numbers as stated before? have a great day!

网友答案:

Use Enum:

public enum Rank {
    DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, 
    NINE, TEN, JACK, QUEEN, KING, ACE;
}

You can add a value if necessary.

Then your array is of type Rank[]

网友答案:

You could make an ENUM for the values, like Two, Three...Jack, Queen, King.

public enum CardValue {
  Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace;
}
  

If you really want ints and Strings in an array, you could use Polymorphism like so:

Object[] deck = new Object[13];
deck[0] = (Object)new Integer(2);
...
deck[12] = (Object)new String("Ace");

You would have to cast everything back to the correct type too when you access it.

String card = (String)deck[12];
int anotherCard = (Integer)deck[0]; 

网友答案:

The right way to do this is to make an enumeration, to this you can assign it's value with which you can calculate your game.

相关阅读:
Top