[英]JAVA blackjack game. String inside of array?
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.
我想知道如何将字符串放置在数组中,或使用String数组并在数组之外为其分配值。 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
: 使用
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[]
那么您的数组的类型为
Rank[]
You could make an ENUM for the values, like Two, Three...Jack, Queen, King. 您可以为这些值制作一个ENUM,例如2、3 ... 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: 如果您确实需要数组中的整数和字符串,则可以使用Polymorphism,如下所示:
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];
正确的方法是进行枚举,为此您可以分配它的值来计算游戏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.