使用2个不同的ENUMS在Java中构建一张卡片 [英] Building a deck of cards in java using 2 different ENUMS
问题描述
public enum Rank {
ACE,
TWO,
三,
四,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING;}
和
public enum Suit {
CLUBS,
SPADES,
HEARTS,
DIAMONDS;}
现在,我需要将它们组合成一个名为Deck的数组,其原理如下:
public Deck(){
cards = new Card [52];
}
要将卡片放入桌面,我一直在尝试使用价值,但我不能正确。这个实验室有一个测试代码,它说我没有得到任何要点。我做错了什么?
public void fill(){
for(int i = 0; i < ; i ++){
for(Suit s:Suit.values()){
for(Rank r:Rank.values()){
cards [i] = new Card(r,或多个);
}
}
}
}
任何帮助非常感激谢谢!
你说,
现在,我需要将它们组合成一个名为Deck的数组,就是这样:
不,你需要创建一个每个枚举有一个字段的类卡。只有这样,你可以创建一个卡片的甲板。所以这样做 - 创建一个卡类,给它至少两个字段,每个枚举一个,加上一个适当的构造函数,加上getter,加上一个体面的 toString()
和那么你设置了。
另外这是错误的:
public void fill(){
for(int i = 0; i <52; i ++){//摆脱这个循环
for(西装s:Suit.values()){
for(Rank r:Rank.values()){
cards [i] = new Card(r,s);
}
上面的代码将尝试将52张卡片填入每个索引点。例如,它将尝试将所有52张卡片卡入[0]卡,相同的卡片[1]项目,只有最后一张卡片将被添加。你会有一个数组的52钻石之王 - 不是你想要的。
相反,摆脱外部循环,而是增加我的循环内:
public void fill(){
int i = 0; $($)$($)$ {
$($)$ {
(b)
i ++; //在这里增加
}
}
}
I have a lab for class (we are allowed to seek outside help) creating klondike solitaire. I am a total noob when it comes to programming (this is my first ever class in programming). We just learned about enums, and I have to build a deck using them (I've reviewed other questions about this, but I haven't found a solution that matches what I need yet). We have two enums (rank and suit):
public enum Rank {
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING;}
and
public enum Suit {
CLUBS,
SPADES,
HEARTS,
DIAMONDS;}
Now, I need to combine them into an array called Deck, which stands as such:
public Deck() {
cards = new Card[52];
}
To put the cards into the deck, I've been trying to use the ".values" but I can't get it right. There is a testing code with this lab and it says I'm not getting any of the points for this. What am I doing wrong?
public void fill() {
for (int i = 0; i<52;i++){
for (Suit s : Suit.values()) {
for (Rank r : Rank.values()) {
cards[i]= new Card(r,s);
}
}
}
}
Any help is much appreciated. Thanks!
You state,
Now, I need to combine them into an array called Deck, which stands as such:
No, you need to create a class Card that has one field of each of the enums. Only after doing that can you create a Deck of your Cards. So do that -- create a Card class, give it at least two fields, one for each enum, plus an appropriate constructor, plus getters, plus a decent toString()
and then you're set.
Also, this is wrong:
public void fill() {
for (int i = 0; i<52;i++){ // get rid of this loop
for (Suit s : Suit.values()) {
for (Rank r : Rank.values()) {
cards[i]= new Card(r,s);
}
The code above will try to stuff 52 cards into each index spot. For instance, it will try to stuff all 52 cards into the cards[0] spot, same for the cards[1] item, and only the last Card will be added. You'll have an array of 52 King of Diamonds -- not what you want.
Instead, get rid of the outer loop, and instead increment the i inside your loop:
public void fill() {
int i = 0;
for (Suit s : Suit.values()) {
for (Rank r : Rank.values()) {
cards[i]= new Card(r,s);
i++; // increment i here
}
}
}
这篇关于使用2个不同的ENUMS在Java中构建一张卡片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!