本文介绍了使用2个不同的ENUMS在Java中构建一张卡片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个课堂实验室(我们被允许寻求外部帮助)创建klondike纸牌。编程时我总是noob(这是我第一个编程类)。我们刚刚了解了枚举,我必须使用它们来构建一个卡座(我已经回顾过关于这个的其他问题,但是我还没有找到一个匹配我需要的解决方案)。我们有两个枚举(等级和诉讼): 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 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 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;}andpublic 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中构建一张卡片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-25 22:28
查看更多