好的,我现在的问题是我的老师要我们上3节课。一个班级(PlayingCard)返回一个等级并适合我开始工作的每张卡片。另一个,deckofplayingcards,应该使用52个扑克牌对象的数组的2个实例变量,以及牌组中顶牌的索引。我的构造函数虽然给了我一些问题。我将我的值从PlayingCard传递回字符串,我需要声明一组唯一的对象卡作为这些字符串。至少可以说它不起作用。我附上了他特别要求的照片。 H.W. assignment descirption
我现在的问题是,我无法使一个对象数组(在代码末尾的DeckOfPlayingCards中)等于一个字符串(在PlayingCard中的方法finalValue中生成)。有什么帮助吗?此外,我的H.W./code的任何其他更正或提示将不胜感激。这是我的代码。当正确地处理像心脏一样的东西时它会返回。 游戏牌:
public class PlayingCard {
private String suit;
private String rank;
public String getSuit(String suitCounter){
String newSuit = null;
switch(suitCounter){
case("S"):
newSuit = "Spades";
break;
case("H"):
newSuit = "Hearts";
break;
case("C"):
newSuit = "Clubs";
break;
case("D"):
newSuit = "Diamonds";
break;
}
suit = newSuit;
return suit;
}
public String getValue(int valueCounter){
String newValue = null;
switch(valueCounter){
case(1):
newValue = "Ace";
break;
case(2):
newValue = "2";
break;
case(3):
newValue = "3";
break;
case(4):
newValue = "4";
break;
case(5):
newValue = "5";
break;
case(6):
newValue = "6";
break;
case(7):
newValue = "7";
break;
case(8):
newValue = "8";
break;
case(9):
newValue = "9";
break;
case(10):
newValue = "10";
break;
case(11):
newValue = "Jack";
break;
case(12):
newValue = "Queen";
break;
case(13):
newValue = "King";
}
rank = newValue;
return rank;
}
public String finalValue(String passedSuit, int passedValue){
String newSuit = getSuit(passedSuit);
String newValue = getValue(passedValue);
return newValue + " of " + newSuit
}
}
DeckOfPlayingCards
public class DeckOfPlayingCards {
PlayingCard[] card = new PlayingCard[52];
PlayingCard cardTop = new PlayingCard();
DeckOfPlayingCards(){
cardTop = card[0];
String randomNumberResult=null;
String randomNum1 = null;
String newval=null;
int randomNumGen1 = (int)(Math.random() * (4-1))+1;
if(randomNumGen1==1){
newval = "H";
}
else if(randomNumGen1==2){
newval = "S";
}
else if(randomNumGen1==3){
newval = "C";
}
else if(randomNumGen1==4){
newval = "D";
}
randomNum1 = newval;
int randomNumGen2 = (int)(Math.random() * (13-1))+1;
int randomNum2 = randomNumGen2;
PlayingCard finalVal = new PlayingCard();
for(int i=0; i<card.length; i++){
card[i] = finalVal.finalValue(randomNum1, randomNum2);
}
}
}
答案 0 :(得分:0)
你不应该将'final value'字符串保存到数组中,而是保存在卡中。由于数组已填充PlayingCard
,因此您应修改方法,将新套装/值分配给班级中的字段,以便在需要时随时拨打finalValue()
。
我建议用Map实现替换你的switch语句,以便快速访问和更清晰的代码片段(如果你还没有学过这些,那么我不建议把它们放在家庭作业中)。正如@Hovercraft Full of Eels所说,你的代码有点复杂,应该使用构造函数而不是mutator(setter)。